Commit ad92917af77a3937a520d6638e79913b12315d77

Authored by Evandro Junior
1 parent 96006ac8

New navbars tests

Showing 1 changed file with 81 additions and 47 deletions   Show diff stats
src/app/components/navbar/navbar.spec.ts
@@ -18,54 +18,55 @@ import { @@ -18,54 +18,55 @@ import {
18 provide 18 provide
19 } from "ng-forward"; 19 } from "ng-forward";
20 20
  21 +import {Session, AuthService, AuthController, IAuthEvents} from "./../auth";
21 22
22 describe("Components", () => { 23 describe("Components", () => {
23 24
24 -  
25 describe("Navbar Component", () => { 25 describe("Navbar Component", () => {
26 26
27 - let $rootScope: ng.IRootScopeService;  
28 - let user = <User>{  
29 - id: 1,  
30 - login: "user"  
31 - };  
32 - 27 + let $rootScope: ng.IRootScopeService;
  28 +
  29 + let user = <User>{
  30 + id: 1,
  31 + login: "user"
  32 + };
  33 +
  34 + let scope = {
  35 + eventCalledHook: () => { },
  36 + $on: (eventName: string, func: Function) => {
  37 + this.eventCalledHook = func;
  38 + }
  39 + }
33 40
34 - let scope = {  
35 - eventCalledHook: () => { },  
36 - $on: (eventName: string, func: Function) => {  
37 - this.eventCalledHook = func; 41 + let modalInstance = {
  42 + close: () => { }
38 } 43 }
39 - }  
40 44
41 - let modalInstance = {  
42 - close: () => { }  
43 - } 45 + let $modal = {
  46 + open: (args: {}) => {
  47 + return modalInstance;
  48 + }
  49 + }
44 50
45 - let $modal = {  
46 - open: (args: {}) => {  
47 - return modalInstance; 51 + let authService = {
  52 + logout: () => { }
48 } 53 }
49 - }  
50 -  
51 - let authService = {  
52 - logout: () => { }  
53 - }  
54 -  
55 - let stateService = jasmine.createSpyObj("$state", ["go"]);  
56 - let providers = [  
57 - new Provider('moment', { useValue: {} }),  
58 - new Provider('$modal', { useValue: $modal }),  
59 - new Provider('AuthService', { useValue: authService }),  
60 - new Provider('Session', {  
61 - useValue: {  
62 - currentUser: () => { return user }  
63 - }  
64 - }),  
65 - new Provider('$scope', { useValue: scope }),  
66 - new Provider('$state', { useValue: stateService }),  
67 - new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } })  
68 - ]; 54 +
  55 + let stateService = jasmine.createSpyObj("$state", ["go"]);
  56 + let providers = [
  57 + new Provider('moment', { useValue: {} }),
  58 + new Provider('$modal', { useValue: $modal }),
  59 + new Provider('AuthService', { useValue: authService }),
  60 + new Provider('Session', {
  61 + useValue: {
  62 + currentUser: () => { return user }
  63 + }
  64 + }),
  65 + new Provider('$scope', { useValue: scope }),
  66 + new Provider('$state', { useValue: stateService }),
  67 + new Provider('AUTH_EVENTS', { useValue: { AUTH_EVENTS } })
  68 + ];
  69 +
69 70
70 beforeEach(angular.mock.module("templates")); 71 beforeEach(angular.mock.module("templates"));
71 72
@@ -76,7 +77,6 @@ describe(&quot;Components&quot;, () =&gt; { @@ -76,7 +77,6 @@ describe(&quot;Components&quot;, () =&gt; {
76 it('should get the loggedIn user', (done: Function) => { 77 it('should get the loggedIn user', (done: Function) => {
77 78
78 let scope = jasmine.createSpyObj("scope", ["$on"]); 79 let scope = jasmine.createSpyObj("scope", ["$on"]);
79 -  
80 let providers = [ 80 let providers = [
81 provideEmptyObjects('moment', '$modal', 'AuthService', '$state'), 81 provideEmptyObjects('moment', '$modal', 'AuthService', '$state'),
82 new Provider('Session', { 82 new Provider('Session', {
@@ -108,7 +108,8 @@ describe(&quot;Components&quot;, () =&gt; { @@ -108,7 +108,8 @@ describe(&quot;Components&quot;, () =&gt; {
108 }); 108 });
109 }); 109 });
110 110
111 - it('It should open on click', (done: Function) => { 111 + it('should open on click', (done: Function) => {
  112 +
112 quickCreateComponent({ 113 quickCreateComponent({
113 providers: providers, 114 providers: providers,
114 template: "<acme-navbar></acme-navbar>", 115 template: "<acme-navbar></acme-navbar>",
@@ -119,17 +120,18 @@ describe(&quot;Components&quot;, () =&gt; { @@ -119,17 +120,18 @@ describe(&quot;Components&quot;, () =&gt; {
119 spyOn($modal, "open"); 120 spyOn($modal, "open");
120 navbarComp.openLogin(); 121 navbarComp.openLogin();
121 expect($modal.open).toHaveBeenCalled(); 122 expect($modal.open).toHaveBeenCalled();
122 - // expect($modal.open).toHaveBeenCalledWith({  
123 - // templateUrl: 'app/components/auth/login.html',  
124 - // controller: 'AuthController',  
125 - // controllerAs: 'vm',  
126 - // bindToController: true  
127 - // }) 123 + expect($modal.open).toHaveBeenCalledWith({
  124 + templateUrl: 'app/components/auth/login.html',
  125 + controller: AuthController,
  126 + controllerAs: 'vm',
  127 + bindToController: true
  128 + })
128 done(); 129 done();
129 }) 130 })
130 }); 131 });
131 132
132 - it('It should logout', (done: Function) => { 133 + it('should logout', (done: Function) => {
  134 +
133 quickCreateComponent({ 135 quickCreateComponent({
134 providers: providers, 136 providers: providers,
135 template: "<acme-navbar></acme-navbar>", 137 template: "<acme-navbar></acme-navbar>",
@@ -145,6 +147,38 @@ describe(&quot;Components&quot;, () =&gt; { @@ -145,6 +147,38 @@ describe(&quot;Components&quot;, () =&gt; {
145 }); 147 });
146 148
147 149
  150 + it('should not activate user when logged in', (done: Function) => {
  151 + quickCreateComponent({
  152 + providers: providers,
  153 + template: "<acme-navbar></acme-navbar>",
  154 + directives: [Navbar]
  155 + })
  156 + .then(fixture => {
  157 + let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
  158 + spyOn(navbarComp, "openLogin");
  159 + navbarComp.activate();
  160 + expect(navbarComp.openLogin.calls.count()).toBe(0);
  161 + done();
  162 + })
  163 +
  164 + });
  165 +
  166 + it('should activate when user not logged in', (done: Function) => {
  167 + user = null;
  168 + quickCreateComponent({
  169 + providers: providers,
  170 + template: "<acme-navbar></acme-navbar>",
  171 + directives: [Navbar]
  172 + })
  173 + .then(fixture => {
  174 + let navbarComp: Navbar = <Navbar>fixture.debugElement.componentViewChildren[0].componentInstance;
  175 + spyOn(navbarComp, "openLogin");
  176 + navbarComp.activate();
  177 + expect(navbarComp.openLogin).toHaveBeenCalled();
  178 + done();
  179 + })
  180 + });
  181 +
148 182
149 // it('closes the modal the login', (done: Function) => { 183 // it('closes the modal the login', (done: Function) => {
150 // let scope = { 184 // let scope = {