dimanche 4 octobre 2015

How can I trigger ui-sref in an AngularJS unit test?

I'm testing an AngularJS view. There are a few non-standard helpers in this code sample, but hopefully it will get the point across. I'm fairly certain none of them are interfering with the function in question.

Here's the view (in Jade):

#authentication-options
  button#sign-up(ui-sref='sign-up') Create an Account
  a#sign-in(ui-sref='sign-in') I already have an account.

And the spec:

spec 'AuthenticationOptions', ->
  beforeEach ->
    @view = $view 'authentication-options.html'

  describe 'elements', ->
    describe 'sign up button', ->
      beforeEach ->
        @button = findSelector 'button#sign-up', in: @view

      it 'exists', ->
        expectElement @button

      # I would expect to be able to test this using
      # `triggerHandler`, but it doesn't work.
      it 'transitions to sign up when clicked', ->
        # @button is the #sign-up button node wrapped with angular.element.

        @button.triggerHandler('click')
        get('$rootScope').$digest()
        expect(get('$state').current.name).to.eq 'sign-up'

This works in the browser but the spec does not change the state. (I receive: AssertionError: expected 'authentication-options' to equal 'sign-up')

The whole module, including ui-router and configured states, is all loaded.

Is there another event I should be firing here, or some other method to test the behavior of this button in a unit/integration test rather than a full-blown E2E?

Aucun commentaire:

Enregistrer un commentaire