diff --git a/src/components/auth/loginUser.functional.spec.js b/src/components/auth/loginUser.functional.spec.js new file mode 100644 index 0000000..21c1074 --- /dev/null +++ b/src/components/auth/loginUser.functional.spec.js @@ -0,0 +1,84 @@ +import signInModule from './signin/signin'; +import signInCtrl from './signin/signin.controller'; +import signInTemplate from './signin/signin.html'; +import directives from '../../directives/email'; +import app from './../../index.js'; + +describe('User log in', function() { + let $rootScope; + let makeController; + let makeTemplate; + let $toast; + let $state; + let $auth; + let $compile; + let form; + let $httpBackend; + + beforeEach(angular.mock.module('app')); + + beforeEach(inject((_$rootScope_, _$toast_, _$state_, _$auth_, _$compile_, _$httpBackend_) => { + $rootScope = _$rootScope_; + + $httpBackend = _$httpBackend_; + + $toast = _$toast_; + + $state = _$state_; + + $auth = _$auth_; + + $compile = _$compile_; + + makeTemplate = angular.element(signInTemplate); + + $compile(makeTemplate)($rootScope); + + form = $rootScope.signin.form; + + makeController = () => { + return new signInCtrl($auth, $state, $toast); + }; + })); + describe('when login with incorrect email and password', function() { + it('should let controller.incorrect be true and form.$submitted be false', function() { + const controller = makeController(); + const data = { email: 'chaoen.l@inwinstack.com', password: 'abc123'}; + controller.form = { '$submitted': true }; + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/login', data).respond(401); + controller.submit(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(controller.form.$submitted).to.eq(false); + expect(controller.incorrect).to.eq(true); + }); + }); + describe('when login with correct email and password', function() { + it('should invoke $state.go and called with dashboard', function() { + const controller = makeController(); + const data = { email: 'chaoen.l@inwinstack.com', password: 'abc1234'}; + const state = sinon.spy($state, 'go'); + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/login', data).respond(200); + controller.submit(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(state).to.have.been.calledWith('dashboard'); + }); + it('should invoke $toast.show and called with Sign In Success!', function(){ + const controller = makeController(); + const data = { email: 'chaoen.l@inwinstack.com', password: 'abc1234'}; + const toast = sinon.spy($toast, 'show'); + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/login', data).respond(200); + controller.submit(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(toast).to.have.been.calledWith('Sign In Success!'); + }); + }); +}); + + + diff --git a/src/components/auth/registerUser.functional.spec.js b/src/components/auth/registerUser.functional.spec.js new file mode 100644 index 0000000..02ff43c --- /dev/null +++ b/src/components/auth/registerUser.functional.spec.js @@ -0,0 +1,112 @@ +import signUpModule from './signup/signup'; +import signUpCtrl from './signup/signup.controller'; +import signUpTemplate from './signup/signup.html'; +import directives from '../../directives/email'; +import app from './../../index.js'; + +describe('User sign up', function() { + let $rootScope; + let makeController; + let $toast; + let $state; + let $auth; + let $compile; + let form; + let $http; + let AuthService; + let $httpBackend; + + beforeEach(angular.mock.module('app')); + + beforeEach(inject((_$rootScope_, _$toast_, _$state_, _$auth_, _$compile_, _AuthService_, _$httpBackend_) => { + $rootScope = _$rootScope_; + + $httpBackend = _$httpBackend_; + + $toast = _$toast_; + + $state = _$state_; + + $auth = _$auth_; + + $compile = _$compile_; + + AuthService = _AuthService_; + + $compile(signUpTemplate)($rootScope); + + form = $rootScope.signup.form; + + makeController = () => { + return new signUpCtrl($auth, $state, $toast, AuthService); + }; + })); + describe('when fill a exist email', function() { + it('should let emailIsValid be false and emailIsInValid be true', function() { + const controller = makeController(); + controller.form = form; + controller.form.email.$setViewValue = 'chaoen@inwinstack.com'; + const data = { email: controller.form.email.$viewValue }; + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/checkEmail', data).respond(403); + controller.checkEmail(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(controller.emailIsValid).to.eq(false); + expect(controller.emailIsInvalid).to.eq(true); + }) + }) + describe('when fill a non-exist email', function() { + it('should let emailIsValid be ture and emailIsInValid be false', function() { + const controller = makeController(); + controller.form = form; + controller.form.email.$setViewValue= 'chaoen.l@inwinstack.com'; + const data = { email: controller.form.email.$viewValue }; + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/checkEmail', data).respond(200); + controller.checkEmail(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(controller.emailIsValid).to.eq(true); + expect(controller.emailIsInvalid).to.eq(false); + }) + }) + describe('when sign up success', function() { + it('should invoke $state.go and called by auth.signin', function() { + const controller = makeController(); + const state = sinon.spy($state, 'go'); + const data = { email: 'chaoen@inwinstack.com', password: 'abc1234' }; + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/register', data).respond(200); + controller.submit(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(state).to.have.been.calledWith('auth.signin'); + }); + it('should invoke $toast.show and called by Sign Up Success!', function() { + const controller = makeController(); + const toast = sinon.spy($toast, 'show'); + const data = { email: 'chaoen@inwinstack.com', password: 'abc1234' }; + controller.credentials = data; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/register', data).respond(200); + controller.submit(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(toast).to.have.been.calledWith('Sign Up Success!'); + }); + }); + describe('when sign up fail', function() { + it('should let form.$submitted false', function() { + const controller = makeController(); + const data = { email: 'chaoen.l@inwinstack.com', password: 'abc1234' }; + controller.credentials = data; + controller.form = form; + controller.form.$submitted = true; + $httpBackend.expectPOST('http://163.17.136.83:8080/api/v1/auth/register', data).respond(403); + controller.submit(); + $httpBackend.flush(); + $rootScope.$digest(); + expect(controller.form.$submitted).to.eq(false); + }); + }); +})