From 7132e26f8a0eb75fe68f96cf9b11a73964356de4 Mon Sep 17 00:00:00 2001 From: Matthew Ross Date: Wed, 11 Apr 2018 17:21:27 -0400 Subject: [PATCH] Unit tests --- package-lock.json | 20 ++-- package.json | 4 +- .../user-settings/user-settings.service.ts | 1 - .../context-menu/context-menu.component.ts | 2 +- src/app/shared/modal/modal.component.ts | 2 +- test/app/board/board.service.spec.ts | 2 +- test/app/mocks.ts | 1 + .../auto-actions/auto-actions.service.spec.ts | 2 +- .../board-admin/board-admin.service.spec.ts | 2 +- test/app/settings/settings.service.spec.ts | 2 +- .../user-admin/user-admin.component.spec.ts | 5 +- .../user-admin/user-admin.service.spec.ts | 2 +- .../user-settings.service.spec.ts | 84 ++++++++------ test/app/shared/auth/auth.guard.spec.js | 19 ---- test/app/shared/auth/auth.guard.spec.ts | 38 +++++++ test/app/shared/auth/auth.service.spec.js | 44 -------- test/app/shared/auth/auth.service.spec.ts | 106 ++++++++++++++++++ .../shared/compile/compile.directive.spec.js | 28 ----- .../context-menu.component.spec.js | 32 ------ .../context-menu.component.spec.ts | 66 +++++++++++ .../context-menu/context-menu.service.spec.js | 30 ----- .../context-menu/context-menu.service.spec.ts | 37 ++++++ .../inline-edit/inline-edit.component.spec.js | 42 ------- .../inline-edit/inline-edit.component.spec.ts | 57 ++++++++++ test/app/shared/modal/modal.component.spec.js | 66 ----------- test/app/shared/modal/modal.component.spec.ts | 67 +++++++++++ test/app/shared/modal/modal.service.spec.js | 62 ---------- test/app/shared/modal/modal.service.spec.ts | 0 28 files changed, 445 insertions(+), 378 deletions(-) delete mode 100644 test/app/shared/auth/auth.guard.spec.js create mode 100644 test/app/shared/auth/auth.guard.spec.ts delete mode 100644 test/app/shared/auth/auth.service.spec.js create mode 100644 test/app/shared/auth/auth.service.spec.ts delete mode 100644 test/app/shared/compile/compile.directive.spec.js delete mode 100644 test/app/shared/context-menu/context-menu.component.spec.js create mode 100644 test/app/shared/context-menu/context-menu.component.spec.ts delete mode 100644 test/app/shared/context-menu/context-menu.service.spec.js create mode 100644 test/app/shared/context-menu/context-menu.service.spec.ts delete mode 100644 test/app/shared/inline-edit/inline-edit.component.spec.js create mode 100644 test/app/shared/inline-edit/inline-edit.component.spec.ts delete mode 100644 test/app/shared/modal/modal.component.spec.js create mode 100644 test/app/shared/modal/modal.component.spec.ts delete mode 100644 test/app/shared/modal/modal.service.spec.js create mode 100644 test/app/shared/modal/modal.service.spec.ts diff --git a/package-lock.json b/package-lock.json index 3f9a106..2a80818 100644 --- a/package-lock.json +++ b/package-lock.json @@ -32,7 +32,7 @@ "requires": { "ajv": "5.5.2", "chokidar": "1.7.0", - "rxjs": "5.5.8", + "rxjs": "5.5.9", "source-map": "0.5.7" }, "dependencies": { @@ -57,7 +57,7 @@ "dev": true, "requires": { "@ngtools/json-schema": "1.2.0", - "rxjs": "5.5.8" + "rxjs": "5.5.9" } }, "@angular/animations": { @@ -117,7 +117,7 @@ "postcss-url": "7.3.2", "raw-loader": "0.5.1", "resolve": "1.5.0", - "rxjs": "5.5.8", + "rxjs": "5.5.9", "sass-loader": "6.0.7", "semver": "5.5.0", "silent-error": "1.1.0", @@ -270,7 +270,7 @@ "integrity": "sha512-7aVP4994Hu8vRdTTohXkfGWEwLhrdNP3EZnWyBootm5zshWqlQojUGweZe5zwewsKcixeVOiy2YtW+aI4aGSLA==", "dev": true, "requires": { - "rxjs": "5.5.8", + "rxjs": "5.5.9", "semver": "5.5.0", "semver-intersect": "1.3.1" } @@ -291,9 +291,9 @@ } }, "@types/node": { - "version": "9.6.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-9.6.2.tgz", - "integrity": "sha512-UWkRY9X7RQHp5OhhRIIka58/gVVycL1zHZu0OTsT5LI86ABaMOSbUjAl+b0FeDhQcxclrkyft3kW5QWdMRs8wQ==", + "version": "9.6.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-9.6.4.tgz", + "integrity": "sha512-Awg4BcUYiZtNKoveGOu654JVPt11V/KIC77iBz8NweyoOAZpz5rUJfPDwwD+ajfTs2HndbTCEB8IuLfX9m/mmw==", "dev": true }, "@types/q": { @@ -9388,9 +9388,9 @@ } }, "rxjs": { - "version": "5.5.8", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-5.5.8.tgz", - "integrity": "sha512-Bz7qou7VAIoGiglJZbzbXa4vpX5BmTTN2Dj/se6+SwADtw4SihqBIiEa7VmTXJ8pynvq0iFr5Gx9VLyye1rIxQ==", + "version": "5.5.9", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-5.5.9.tgz", + "integrity": "sha512-DHG9AHmCmgaFWgjBcXp6NxFDmh3MvIA62GqTWmLnTzr/3oZ6h5hLD8NA+9j+GF0jEwklNIpI4KuuyLG8UWMEvQ==", "requires": { "symbol-observable": "1.0.1" } diff --git a/package.json b/package.json index 47118e2..5515767 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "highlight.js": "^9.12.0", "marked": "^0.3.19", "ng2-dragula": "^1.5.0", - "rxjs": "^5.5.8", + "rxjs": "^5.5.9", "scss-base": "^1.4.0", "zone.js": "^0.8.26" }, @@ -62,7 +62,7 @@ "@angular/language-service": "^5.2.9", "@types/jasmine": "~2.8.6", "@types/jasminewd2": "~2.0.3", - "@types/node": "~9.6.2", + "@types/node": "~9.6.4", "bourbon": "5.0.0", "bourbon-neat": "1.9.0", "codelyzer": "^4.2.1", diff --git a/src/app/settings/user-settings/user-settings.service.ts b/src/app/settings/user-settings/user-settings.service.ts index 3cf1749..735500f 100644 --- a/src/app/settings/user-settings/user-settings.service.ts +++ b/src/app/settings/user-settings/user-settings.service.ts @@ -80,7 +80,6 @@ export class UserSettingsService { return this.http.post('api/users/' + this.activeUser.id, json) .map((response: ApiResponse) => { this.auth.updateUser(JSON.parse(response.data[1])); - console.log(response); return response; }) diff --git a/src/app/shared/context-menu/context-menu.component.ts b/src/app/shared/context-menu/context-menu.component.ts index 22570c8..13c1ef1 100644 --- a/src/app/shared/context-menu/context-menu.component.ts +++ b/src/app/shared/context-menu/context-menu.component.ts @@ -17,7 +17,7 @@ export class ContextMenu { isOpen = false; - constructor(private el: ElementRef, + constructor(public el: ElementRef, private menuService: ContextMenuService, private sanitizer: DomSanitizer) { menuService.registerMenu(this); diff --git a/src/app/shared/modal/modal.component.ts b/src/app/shared/modal/modal.component.ts index e728e5f..5c2f072 100644 --- a/src/app/shared/modal/modal.component.ts +++ b/src/app/shared/modal/modal.component.ts @@ -27,7 +27,7 @@ export class Modal implements OnInit { isOpen = false; animate = true; - constructor(private modalService: ModalService) { + constructor(public modalService: ModalService) { } ngOnInit() { diff --git a/test/app/board/board.service.spec.ts b/test/app/board/board.service.spec.ts index f1ff736..241637e 100644 --- a/test/app/board/board.service.spec.ts +++ b/test/app/board/board.service.spec.ts @@ -214,7 +214,7 @@ describe('BoardService', () => { } else { req.flush({ data: [] }); } - } + }; }); diff --git a/test/app/mocks.ts b/test/app/mocks.ts index c031b11..5519671 100644 --- a/test/app/mocks.ts +++ b/test/app/mocks.ts @@ -96,6 +96,7 @@ export class AuthServiceMock { language: 'en' }; + authenticate () { return new BehaviorSubject(true); } updateUser () { } } diff --git a/test/app/settings/auto-actions/auto-actions.service.spec.ts b/test/app/settings/auto-actions/auto-actions.service.spec.ts index 732bee5..e167bf9 100644 --- a/test/app/settings/auto-actions/auto-actions.service.spec.ts +++ b/test/app/settings/auto-actions/auto-actions.service.spec.ts @@ -73,7 +73,7 @@ describe('AutoActionsService', () => { } else { req.flush({ data: [] }); } - } + }; }); diff --git a/test/app/settings/board-admin/board-admin.service.spec.ts b/test/app/settings/board-admin/board-admin.service.spec.ts index 1daf866..2f78fd6 100644 --- a/test/app/settings/board-admin/board-admin.service.spec.ts +++ b/test/app/settings/board-admin/board-admin.service.spec.ts @@ -89,7 +89,7 @@ describe('BoardAdminService', () => { } else { req.flush({ data: [] }); } - } + }; }); diff --git a/test/app/settings/settings.service.spec.ts b/test/app/settings/settings.service.spec.ts index a47c533..69c0a73 100644 --- a/test/app/settings/settings.service.spec.ts +++ b/test/app/settings/settings.service.spec.ts @@ -124,7 +124,7 @@ describe('SettingsService', () => { } else { req.flush({ data: [] }); } - } + }; }); diff --git a/test/app/settings/user-admin/user-admin.component.spec.ts b/test/app/settings/user-admin/user-admin.component.spec.ts index ccfbc77..75ab818 100644 --- a/test/app/settings/user-admin/user-admin.component.spec.ts +++ b/test/app/settings/user-admin/user-admin.component.spec.ts @@ -19,7 +19,7 @@ import { User } from '../../../../src/app/shared/models'; import { SettingsServiceMock, AuthServiceMock } from '../../mocks'; import { - UserDisplay + UserDisplay, ModalUser } from '../../../../src/app/settings/user-admin/user-admin.models'; import { UserAdmin @@ -94,6 +94,9 @@ describe('UserAdmin', () => { (component.userService).editUser = () => { return { subscribe: fn => { const user = new User(); + user.board_access = [1]; + const mUser = new ModalUser(user); + fn({ status: 'success', alerts: [{}], data: [{}, JSON.stringify([user])] }); diff --git a/test/app/settings/user-admin/user-admin.service.spec.ts b/test/app/settings/user-admin/user-admin.service.spec.ts index 96d2b1a..650ca35 100644 --- a/test/app/settings/user-admin/user-admin.service.spec.ts +++ b/test/app/settings/user-admin/user-admin.service.spec.ts @@ -89,7 +89,7 @@ describe('UserAdminService', () => { } else { req.flush({ data: [] }); } - } + }; }); diff --git a/test/app/settings/user-settings/user-settings.service.spec.ts b/test/app/settings/user-settings/user-settings.service.spec.ts index aafe828..58f6627 100644 --- a/test/app/settings/user-settings/user-settings.service.spec.ts +++ b/test/app/settings/user-settings/user-settings.service.spec.ts @@ -40,7 +40,7 @@ describe('UserSettingsService', () => { it('changes default board', () => { service.changeDefaultBoard({}).subscribe(response => { - expect(response.data.length).toEqual(2); + expect(response.data.length).toEqual(3); }); testCall('api/users/1', 'POST'); @@ -56,7 +56,7 @@ describe('UserSettingsService', () => { it('changes password', () => { service.changePassword('', '').subscribe(response => { - expect(response.data.length).toEqual(2); + expect(response.data.length).toEqual(3); }); testCall('api/users/1', 'POST'); @@ -70,37 +70,53 @@ describe('UserSettingsService', () => { testCall('api/users/1', 'POST', true); }); - // it('changes username', () => { - // service.changeUsername('').subscribe(response => { - // expect(response.data.length).toEqual(2); - // }); - // - // testCall('api/users/1', 'POST'); - // }); - // - // it('handles errors on change username', () => { - // service.changeUsername('').subscribe(() => {}, response => { - // expect(response.alerts.length).toEqual(1); - // }); - // - // testCall('api/users/1', 'POST', true); - // }); + it('changes username', () => { + service.changeUsername('').subscribe(response => { + expect(response.data.length).toEqual(3); + }); - // it('changes email', () => { - // service.changeEmail('').subscribe(response => { - // expect(response.data.length).toEqual(2); - // }); - // - // testCall('api/users/1', 'POST'); - // }); - // - // it('handles errors on change email', () => { - // service.changeEmail('').subscribe(() => {}, response => { - // expect(response.alerts.length).toEqual(1); - // }); - // - // testCall('api/users/1', 'POST', true); - // }); + testCall('api/users/1', 'POST'); + }); + + it('handles errors on change username', () => { + service.changeUsername('').subscribe(() => {}, response => { + expect(response.alerts.length).toEqual(1); + }); + + testCall('api/users/1', 'POST', true); + }); + + it('changes email', () => { + service.changeEmail('').subscribe(response => { + expect(response.data.length).toEqual(3); + }); + + testCall('api/users/1', 'POST'); + }); + + it('handles errors on change email', () => { + service.changeEmail('').subscribe(() => {}, response => { + expect(response.alerts.length).toEqual(1); + }); + + testCall('api/users/1', 'POST', true); + }); + + it('changes user options', () => { + service.changeUserOptions({}).subscribe(response => { + expect(response.data.length).toEqual(3); + }); + + testCall('api/users/1/opts', 'POST'); + }); + + it('handles errors on change user options', () => { + service.changeUserOptions({}).subscribe(() => {}, response => { + expect(response.alerts.length).toEqual(1); + }); + + testCall('api/users/1/opts', 'POST', true); + }); const testCall = (url, method, isError = false) => { const req = httpMock.expectOne(url); @@ -109,9 +125,9 @@ describe('UserSettingsService', () => { if (isError) { req.flush({ alerts: [{}], data: [] }, { status: 500, statusText: '' }); } else { - req.flush({ data: [{}, {}] }); + req.flush({ data: [{}, '{}', '{}'] }); } - } + }; }); diff --git a/test/app/shared/auth/auth.guard.spec.js b/test/app/shared/auth/auth.guard.spec.js deleted file mode 100644 index 511d8d9..0000000 --- a/test/app/shared/auth/auth.guard.spec.js +++ /dev/null @@ -1,19 +0,0 @@ -/* global expect AuthServiceMock */ -var path = '../../../../build/shared/auth/', - AuthGuard = require(path + 'auth.guard.js').AuthGuard; - -describe('AuthGuard', () => { - var authGuard; - - beforeEach(() => { - authGuard = new AuthGuard(AuthServiceMock); - }); - - it('checks a route can activate via the auth service', done => { - authGuard.canActivate().subscribe(isAuth => { - expect(isAuth).to.equal(true); - done(); - }); - }); -}); - diff --git a/test/app/shared/auth/auth.guard.spec.ts b/test/app/shared/auth/auth.guard.spec.ts new file mode 100644 index 0000000..a6946fc --- /dev/null +++ b/test/app/shared/auth/auth.guard.spec.ts @@ -0,0 +1,38 @@ +import { TestBed, getTestBed } from '@angular/core/testing' + +import { AuthGuard } from '../../../../src/app/shared/auth/auth.guard'; +import { AuthService } from '../../../../src/app/shared/services'; +import { AuthServiceMock } from '../../mocks'; + +describe('AuthGuard', () => { + let injector: TestBed; + let service: AuthGuard; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [], + providers: [ + AuthGuard, + { provide: AuthService, useClass: AuthServiceMock } + ] + }); + + injector = getTestBed(); + service = injector.get(AuthGuard); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('implements CanActivate', () => { + let actual = false; + + service.canActivate().subscribe(value => actual = value); + + expect(actual).toEqual(true); + }); + +}); + + diff --git a/test/app/shared/auth/auth.service.spec.js b/test/app/shared/auth/auth.service.spec.js deleted file mode 100644 index 56ec21c..0000000 --- a/test/app/shared/auth/auth.service.spec.js +++ /dev/null @@ -1,44 +0,0 @@ -/* global expect ConstantsMock, HttpMock, RouterMock, StringsServiceMock */ -var path = '../../../../build/shared/auth/', - AuthService = require(path + 'auth.service.js').AuthService; - -describe('AuthService', () => { - var authService; - - beforeEach(() => { - authService = new AuthService(ConstantsMock, - HttpMock, new RouterMock(), StringsServiceMock); - }); - - it('has userOptions', () => { - expect(authService.userOptions).to.equal(null); - }); - - it('has userChanged observable', () => { - expect(authService.userChanged).to.be.an('object'); - }); - - it('calls the API to authenticate a JWT', done => { - authService.authenticate().subscribe(test => { - expect(test).to.equal(true); - done(); - }); - }); - - it('calls the API to log in', done => { - authService.login('user', 'pass', false).subscribe(res => { - expect(res.status).to.equal('success'); - expect(res.endpoint).to.equal('api/login'); - done(); - }); - }); - - it('calls the API to log out', done => { - authService.logout().subscribe(res => { - expect(res.status).to.equal('success'); - expect(res.endpoint).to.equal('api/logout'); - done(); - }); - }); -}); - diff --git a/test/app/shared/auth/auth.service.spec.ts b/test/app/shared/auth/auth.service.spec.ts new file mode 100644 index 0000000..0cbbe2b --- /dev/null +++ b/test/app/shared/auth/auth.service.spec.ts @@ -0,0 +1,106 @@ +import { TestBed, getTestBed } from '@angular/core/testing' +import { + HttpClientTestingModule, + HttpTestingController +} from '@angular/common/http/testing'; +import { RouterTestingModule } from '@angular/router/testing'; + +import { Constants } from '../../../../src/app/shared/constants'; +import { StringsService } from '../../../../src/app/shared/services'; + +import { AuthService } from '../../../../src/app/shared/auth/auth.service'; + +describe('AuthService', () => { + let injector: TestBed; + let service: AuthService; + let httpMock: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + HttpClientTestingModule, + RouterTestingModule.withRoutes([]) + ], + providers: [ + Constants, + StringsService, + AuthService + ] + }); + + injector = getTestBed(); + service = injector.get(AuthService); + httpMock = injector.get(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }) + + it('should be created', () => { + expect(service).toBeTruthy(); + }); + + it('updates a user', () => { + let actual = false; + + service.userChanged.subscribe(() => actual = true); + service.updateUser({}, { id: 1 }); + + expect(actual).toEqual(true); + httpMock.expectOne('strings/en.json'); + }); + + it('authenticates a user', () => { + service.authenticate().subscribe(response => { + expect(response).toEqual(true); + }); + + testCall('api/authenticate', 'POST'); + }); + + it('handles errors on authenticate', () => { + service.authenticate().subscribe(response => { + expect(response).toEqual(false); + }); + + testCall('api/authenticate', 'POST', true); + }); + + it('logs in a user', () => { + service.login('test', 'test', true).subscribe(response => { + expect(response.data.length).toEqual(2); + }); + + testCall('api/login', 'POST'); + }); + + it('handles errors on user login', () => { + service.login('test', 'test', true).subscribe(() => {}, response => { + expect(response.alerts.length).toEqual(1); + }); + + testCall('api/login', 'POST', true); + }); + + it('logs out a user', () => { + service.logout().subscribe(response => { + expect(response.data.length).toEqual(2); + }); + + testCall('api/logout', 'POST'); + }); + + const testCall = (url, method, isError = false) => { + const req = httpMock.expectOne(url); + expect(req.request.method).toEqual(method); + + if (isError) { + req.flush({ alerts: [{}] }, { status: 500, statusText: '' }); + } else { + req.flush({ data: [{}, {}] }); + } + }; + +}); + diff --git a/test/app/shared/compile/compile.directive.spec.js b/test/app/shared/compile/compile.directive.spec.js deleted file mode 100644 index bdcea84..0000000 --- a/test/app/shared/compile/compile.directive.spec.js +++ /dev/null @@ -1,28 +0,0 @@ -/* global expect RxJs */ -var path = '../../../../build/shared/compile/', - CompileDirective = require(path + 'compile.directive.js').CompileDirective; - -describe('CompileDirective', () => { - var compile, - vcRef = { - clear: () => {}, - createComponent: () => {} - }, - compiler = { - compileModuleAndAllComponentsAsync: () => { - return RxJs.Observable.of({ componentFactories: [] }).toPromise(); - } - }; - - beforeEach(() => { - compile = new CompileDirective(vcRef, compiler); - compile.compile = '
'; - }); - - it('implements OnChanges', () => { - compile.ngOnChanges(); - - expect(compile.compRef).to.equal(null); - }); -}); - diff --git a/test/app/shared/context-menu/context-menu.component.spec.js b/test/app/shared/context-menu/context-menu.component.spec.js deleted file mode 100644 index d18827a..0000000 --- a/test/app/shared/context-menu/context-menu.component.spec.js +++ /dev/null @@ -1,32 +0,0 @@ -/* global expect ElementRefMock ContextMenuServiceMock SanitizerMock */ -var path = '../../../../build/shared/context-menu/', - ContextMenu = require(path + 'context-menu.component.js').ContextMenu; - -describe('ContextMenu', () => { - var contextMenu, - contextMenuService, - event = { preventDefault: () => {}, stopPropagation: () => {}}; - - beforeEach(() => { - contextMenuService = new ContextMenuServiceMock(); - contextMenu = new ContextMenu(ElementRefMock, contextMenuService, SanitizerMock); - }); - - it('has a function to get menu item text', () => { - expect(contextMenu.getText({ text: 'testing' })).to.equal('testing'); - }); - - it('captures the parent oncontextmenu event', done => { - contextMenuService.closeAllCalled.subscribe( called => { - expect(called).to.equal(true); - done(); - }); - - contextMenu.parentEventHandler({ - pageX: 10, pageY: 10, - preventDefault: () => {}, - stopPropagation: () => {} - }); - }); -}); - diff --git a/test/app/shared/context-menu/context-menu.component.spec.ts b/test/app/shared/context-menu/context-menu.component.spec.ts new file mode 100644 index 0000000..fc2a0a8 --- /dev/null +++ b/test/app/shared/context-menu/context-menu.component.spec.ts @@ -0,0 +1,66 @@ +import { TestBed, ComponentFixture } from '@angular/core/testing' +import { ElementRef } from '@angular/core'; +import { DomSanitizer } from '@angular/platform-browser'; + +import { SharedModule } from '../../../../src/app/shared/shared.module'; + +import { + ContextMenuService +} from '../../../../src/app/shared/context-menu/context-menu.service'; +import { + ContextMenu +} from '../../../../src/app/shared/context-menu/context-menu.component'; + +class ElementRefMock { + public nativeElement = { parentElement: {} }; +} + +describe('ContextMenu', () => { + let component: ContextMenu, + fixture: ComponentFixture, + elementRef: ElementRefMock; + + const getPrivateFunction = name => component[name].bind(component); + + beforeEach(() => { + elementRef = new ElementRefMock(); + + TestBed.configureTestingModule({ + imports: [ SharedModule ], + providers: [ + ContextMenuService, + { provide: ElementRef, useValue: elementRef } + ] + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(ContextMenu); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('can be constructed', () => { + expect(component).toBeTruthy(); + }); + + it('captures parent oncontextmenu events', () => { + let parentElement = component.el.nativeElement.parentElement; + + expect(parentElement.oncontextmenu).toEqual(jasmine.any(Function)); + + parentElement.oncontextmenu({ + preventDefault: () => {}, + stopPropagation: () => {} + }); + }); + + it('gets HTML formatted text from items', () => { + let actual = component.getText({ text: '

Test

' }); + + expect((actual).changingThisBreaksApplicationSecurity) + .toEqual('

Test

'); + }); + +}); + diff --git a/test/app/shared/context-menu/context-menu.service.spec.js b/test/app/shared/context-menu/context-menu.service.spec.js deleted file mode 100644 index 0010cf0..0000000 --- a/test/app/shared/context-menu/context-menu.service.spec.js +++ /dev/null @@ -1,30 +0,0 @@ -/* global expect */ -var path = '../../../../build/shared/context-menu/', - ContextMenuItem = require(path + 'context-menu-item.model.js').ContextMenuItem, - ContextMenuService = require(path + 'context-menu.service.js').ContextMenuService; - -describe('ContextMenu', () => { - var contextMenuService; - - beforeEach(() => { - contextMenuService = new ContextMenuService(); - }); - - it('has a menus array', () => { - expect(contextMenuService.menus).to.be.an('array'); - }); - - it('has a registerMenu function', () => { - contextMenuService.registerMenu(new ContextMenuItem()); - - expect(contextMenuService.menus.length).to.equal(1); - }); - - it('has a closeAllMenus function', () => { - contextMenuService.registerMenu({ isOpen: true }); - contextMenuService.closeAllMenus({}); - - expect(contextMenuService.menus[0].isOpen).to.equal(false); - }); -}); - diff --git a/test/app/shared/context-menu/context-menu.service.spec.ts b/test/app/shared/context-menu/context-menu.service.spec.ts new file mode 100644 index 0000000..e5af817 --- /dev/null +++ b/test/app/shared/context-menu/context-menu.service.spec.ts @@ -0,0 +1,37 @@ +import { + ContextMenuService +} from '../../../../src/app/shared/context-menu/context-menu.service'; + +describe('ContextMenuService', () => { + let service: ContextMenuService; + + beforeEach(() => { + service = new ContextMenuService(); + }); + + it('can be constructed', () => { + expect(service).toBeTruthy(); + }); + + it('can have a menu registered', () => { + service.registerMenu({}); + + expect(service['menus'].length).toEqual(1); + }); + + it('has a way to close all menus', () => { + service.registerMenu({ isOpen: true }); + service.closeAllMenus(); + + expect(service['menus'][0].isOpen).toEqual(false); + }); + + it('closes all menus on document click', () => { + service.registerMenu({ isOpen: true }); + document.body.click(); + + expect(service['menus'][0].isOpen).toEqual(false); + }); + +}); + diff --git a/test/app/shared/inline-edit/inline-edit.component.spec.js b/test/app/shared/inline-edit/inline-edit.component.spec.js deleted file mode 100644 index 5bc93ae..0000000 --- a/test/app/shared/inline-edit/inline-edit.component.spec.js +++ /dev/null @@ -1,42 +0,0 @@ -/* global expect */ -var path = '../../../../build/shared/inline-edit/', - InlineEdit = require(path + 'inline-edit.component.js').InlineEdit; - -describe('InlineEdit', () => { - var inlineEdit; - - beforeEach(() => { - inlineEdit = new InlineEdit(); - }); - - it('has a beginEdit method', done => { - expect(inlineEdit.beginEdit).to.be.a('function'); - - var called = false, - el = { - focus: () => { - called = true; - } - }; - - inlineEdit.beginEdit(el); - expect(inlineEdit.isDisplay).to.equal(false); - - setTimeout(() => { - expect(called).to.equal(true); - done(); - }, 110); - }); - - it('has an editDone function', done => { - expect(inlineEdit.editDone).to.be.a('function'); - - inlineEdit.edit.subscribe(text => { - expect(text).to.equal('test'); - done(); - }); - - inlineEdit.editDone('test', { stopPropagation: () => {} }); - }); -}); - diff --git a/test/app/shared/inline-edit/inline-edit.component.spec.ts b/test/app/shared/inline-edit/inline-edit.component.spec.ts new file mode 100644 index 0000000..f3e8dff --- /dev/null +++ b/test/app/shared/inline-edit/inline-edit.component.spec.ts @@ -0,0 +1,57 @@ +import { TestBed, ComponentFixture } from '@angular/core/testing' + +import { SharedModule } from '../../../../src/app/shared/shared.module'; + +import { + InlineEdit +} from '../../../../src/app/shared/inline-edit/inline-edit.component'; + +describe('InlineEdit', () => { + let component: InlineEdit, + fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ SharedModule ] + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(InlineEdit); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('can be constructed', () => { + expect(component).toBeTruthy(); + }); + + it('toggles display for edit mode', done => { + expect(component.isDisplay).toEqual(true); + + let called = false; + const el = { focus: () => called = true }; + + component.beginEdit(el); + expect(component.isDisplay).toEqual(false); + + setTimeout(() => { + expect(called).toEqual(true); + done(); + }, 110); + }); + + it('updates values when editing is complete', () => { + let called = false; + const event = { stopPropagation: () => called = true }; + + component.editDone('test'); + component.editDone('test', event); + + expect(called).toEqual(true); + expect(component.isDisplay).toEqual(true); + expect(component.text).toEqual('test'); + }); + +}); + diff --git a/test/app/shared/modal/modal.component.spec.js b/test/app/shared/modal/modal.component.spec.js deleted file mode 100644 index b735646..0000000 --- a/test/app/shared/modal/modal.component.spec.js +++ /dev/null @@ -1,66 +0,0 @@ -/* global expect ModalServiceMock */ -var path = '../../../../build/shared/modal/', - Modal = require(path + 'modal.component.js').Modal; - -describe('Modal', () => { - var modal, - modalService; - - beforeEach(() => { - modalService = new ModalServiceMock(); - modal = new Modal(modalService); - }); - - it('has modalId property', () => { - expect(modal.modalId).to.be.a('string'); - }); - - it('has modalTitle property', () => { - expect(modal.modalTitle).to.be.a('string'); - }); - - it('has blocking property', () => { - expect(modal.blocking).to.be.a('boolean'); - }); - - it('has isOpen property', () => { - expect(modal.isOpen).to.be.a('boolean'); - }); - - it('registers itself with the modal service on init', done => { - modalService.registerCalled.subscribe(called => { - expect(called).to.equal(true); - done(); - }); - modal.ngOnInit(); - }); - - it('calls the close function on the service', done => { - modalService.closeCalled.subscribe(called => { - expect(called).to.equal(true); - done(); - }); - modal.close(); - }); - - it('calls close on Escape keypress', done => { - modalService.closeCalled.subscribe(called => { - expect(called).to.equal(true); - done(); - }); - modal.keyup({ keyCode: 27 }); - }); - - it('filters clicks on the modal to prevent closing', () => { - var stopCalled = false, - eventStop = { - stopPropagation: () => { - stopCalled = true; - } - }; - - modal.filterClick(eventStop); - expect(stopCalled).to.equal(true); - }); -}); - diff --git a/test/app/shared/modal/modal.component.spec.ts b/test/app/shared/modal/modal.component.spec.ts new file mode 100644 index 0000000..e044dce --- /dev/null +++ b/test/app/shared/modal/modal.component.spec.ts @@ -0,0 +1,67 @@ +import { TestBed, ComponentFixture } from '@angular/core/testing' +import { RouterTestingModule } from '@angular/router/testing'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { FormsModule } from '@angular/forms'; + +import { SharedModule } from '../../../../src/app/shared/shared.module'; + +import { ModalService } from '../../../../src/app/shared/services'; +import { Modal } from '../../../../src/app/shared/modal/modal.component'; + +fdescribe('Modal', () => { + let component: Modal, + fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + RouterTestingModule, + HttpClientTestingModule, + FormsModule, + SharedModule + ], + providers: [ ModalService ] + }).compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(Modal); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('can be constructed', () => { + expect(component).toBeTruthy(); + }); + + it('calls the modal service to close', () => { + component.modalId = 'MODAL'; + + (component.modalService).close = (id, checkBlocking) => { + expect(id).toEqual('MODAL'); + expect(checkBlocking).toEqual(false); + }; + + component.close(); + + (component.modalService).close = (id, checkBlocking) => { + expect(id).toEqual('MODAL'); + expect(checkBlocking).toEqual(true); + }; + + component.close(true); + }); + + it('filters click events', () => { + window.event = {}; + component.filterClick(null); + + let called = false; + const event = { stopPropagation: () => called = true }; + + component.filterClick(event); + expect(called).toEqual(true); + }); + +}); + diff --git a/test/app/shared/modal/modal.service.spec.js b/test/app/shared/modal/modal.service.spec.js deleted file mode 100644 index 611d9e1..0000000 --- a/test/app/shared/modal/modal.service.spec.js +++ /dev/null @@ -1,62 +0,0 @@ -/* globals expect AuthServiceMock */ -var path = '../../../../build/shared/modal/', - ModalService = require(path + 'modal.service.js').ModalService; - -describe('ModalService', () => { - var modalService, - modal; - - beforeEach(() => { - modalService = new ModalService(AuthServiceMock); - modal = { - modalId: 'testModal', - isOpen: false, - focusElement: { - nativeElement: { - focus: () => {} - } - } - }; - }); - - it('has an array of modals', () => { - expect(modalService.modals).to.be.an('array'); - expect(modalService.modals.length).to.equal(0); - }); - - it('has a function to register a modal', () => { - expect(modalService.registerModal).to.be.a('function'); - - modalService.registerModal(modal); - expect(modalService.modals[0].modalId).to.equal(modal.modalId); - }); - - it('will not register the same modal twice', () => { - modalService.registerModal(modal); - expect(modalService.modals.length).to.equal(1); - - modalService.registerModal(modal); - expect(modalService.modals.length).to.equal(1); - }); - - it('has a function to open a modal', () => { - modalService.registerModal(modal); - modalService.open(modal.modalId); - - expect(modalService.modals[0].isOpen).to.equal(true); - }); - - it('has a function to close a modal', () => { - modal.isOpen = true; - modalService.registerModal(modal); - - modalService.close(modal.modalId); - expect(modalService.modals[0].isOpen).to.equal(false); - - modalService.modals[0].isOpen = true; - modal.blocking = true; - modalService.close(modal.modalId, true); - expect(modalService.modals[0].isOpen).to.equal(true); - }); -}); - diff --git a/test/app/shared/modal/modal.service.spec.ts b/test/app/shared/modal/modal.service.spec.ts new file mode 100644 index 0000000..e69de29