Created
January 19, 2018 04:37
-
-
Save SpencerCooley/f5373ac9fd1a596982777bdf337cb61e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this tests everything I was pretty much trying to test without worrying about status codes in the axios request. | |
describe('LoginForm', () => { | |
it('should trigger attemptLogin on form submit.', ()=>{ | |
const spy = spyOn(cmp.vm, 'attemptLogin') | |
cmp.update() | |
cmp.vm.username = 'spencercooley' | |
cmp.vm.password = 'top_secret' | |
const theForm = cmp.find('form'); | |
theForm.trigger('submit'); | |
expect(cmp.vm.attemptLogin).toBeCalled() | |
}); | |
it('should have invalid set to true on failed login and show the form with an error message.', () =>{ | |
let errorResponse = { | |
'response':{ | |
'data':{ | |
'error': 'invalid_grant', | |
'error_description': 'Invalid credentials given.' | |
} | |
} | |
}; | |
//the method called when status code is 401 | |
cmp.vm.authFailure(errorResponse); | |
cmp.update(); | |
expect(cmp.vm.invalid).toBe(true); | |
expect(cmp.vm.loading).toBe(false); | |
//correct elements are showing. | |
expect(cmp.findAll('form').length).toBe(1); | |
expect(cmp.findAll('.animated-loader').length).toBe(0); | |
//check that an error is present in the dom and the data. | |
expect(cmp.vm.submitErrors.length).toBe(1); | |
expect(cmp.findAll('li.error').length).toBe(1); | |
expect(cmp.vm.submitErrors[0]).toBe('Invalid credentials given.'); | |
}); | |
it('should have invalid set to false on successful login and remove the form.', () =>{ | |
let apiResponse = { | |
'data':{ | |
'access_token': 'uYPlFOapaOaElDmW7V0Aq4mY2szJgo', | |
'scope': 'groups write read', | |
'token_type': 'Bearer', | |
'expires_in': 36000, | |
'refresh_token': 'EtQUYtximygPUPACAHKIY7nVmqKM49' | |
} | |
}; | |
//the method called when status code is 200 | |
cmp.vm.authSuccess(apiResponse); | |
cmp.update(); | |
expect(cmp.vm.invalid).toBe(false); | |
expect(cmp.vm.loading).toBe(true); | |
//correct elements are showing. | |
expect(cmp.findAll('form').length).toBe(0); | |
expect(cmp.findAll('.animated-loader').length).toBe(1); | |
//check that localStorage has been set | |
expect(localStorage.getItem('authToken')).toBe('uYPlFOapaOaElDmW7V0Aq4mY2szJgo') | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment