How do you mock directives to enable unit testing of higher level directive?

前端 未结 7 1848
故里飘歌
故里飘歌 2020-12-04 09:05

In our app we have several layers of nested directives. I\'m trying to write some unit tests for the top level directives. I\'ve mocked in stuff that the directive itself ne

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 09:26

    Here is another small idea. Just put this code in jasmine helpers (coffee script)

    window.mockDirective = (name, factoryFunction) ->
      mockModule = angular.module('mocks.directives', ['ng'])
      mockModule.directive(name, factoryFunction)
    
      module ($provide) ->
        factoryObject = angular.injector([mockModule.name]).get("#{name}Directive")
        $provide.factory "#{name}Directive", -> factoryObject
        null
    

    And use it:

    beforeEach mockDirective, "myLowerLevelDirective", ->
      link: (scope, element) ->
    

    This will completely remove all other implementations of given directive, giving a full access to test passed arguments to the directive. FOr example, mm.foundation alert directive can be mocked with:

    beforeEach mockDirective 'alert', ->
      scope:
        type: '='
    

    and then tested:

    expect(element.find('alert').data('$isolateScopeNoTemplate').type).toEqual 
    

提交回复
热议问题