| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { DistributionGroupSandbox } from '@grid-failure-information-app/app/pages/distribution-group/distribution-group.sandbox'; |
| import { Store, ActionsSubject } from '@ngrx/store'; |
| import * as store from '@grid-failure-information-app/shared/store'; |
| import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| import { of } from 'rxjs'; |
| import { UtilService } from '@grid-failure-information-app/shared/utility/utility.service'; |
| import * as distributionGroupActions from '@grid-failure-information-app/shared/store/actions/distribution-groups.action'; |
| import { Contact, DistributionGroupMember } from '@grid-failure-information-app/shared/models'; |
| |
| describe('DistributionGroupSandbox', () => { |
| let service: DistributionGroupSandbox; |
| let appState: Store<store.State>; |
| let actionsSubject: ActionsSubject; |
| let utilService: UtilService; |
| let modalService: NgbModal; |
| |
| beforeEach(() => { |
| appState = { dispatch: () => {}, pipe: () => of(true), select: () => of(true) } as any; |
| actionsSubject = { dispatch: () => {}, pipe: () => of(true), select: () => of(true) } as any; |
| utilService = { displayNotification() {} } as any; |
| modalService = { open() {} } as any; |
| spyOn(appState, 'dispatch').and.callFake(() => {}); |
| |
| service = new DistributionGroupSandbox(appState, actionsSubject, utilService, modalService); |
| }); |
| |
| it('should create DistributionGroupSandbox service', () => { |
| expect(service).toBeTruthy(); |
| }); |
| |
| it('should set displayForm', () => { |
| service.displayDistributionGroupMember = false; |
| service.openForm(); |
| expect(service.displayForm).toBeTruthy(); |
| }); |
| |
| it('should dispatch loadDistributionGroups Action via loadDistributionGroups()', () => { |
| service.loadDistributionGroups(); |
| expect(appState.dispatch).toHaveBeenCalledWith(distributionGroupActions.loadDistributionGroups()); |
| }); |
| |
| it('should dispatch loadDistributionGroupDetails Action via loadDistributionGroupDetail()', () => { |
| service.loadDistributionGroupDetail('x'); |
| expect(appState.dispatch).toHaveBeenCalledWith(distributionGroupActions.loadDistributionGroupsDetail({ payload: 'x' })); |
| }); |
| |
| it('should dispatch loadDistributionGroupTextPlaceholders Action via loadDistributionGroupTextPlaceholders()', () => { |
| service.loadDistributionGroupTextPlaceholders(); |
| expect(appState.dispatch).toHaveBeenCalledWith(distributionGroupActions.loadDistributionGroupTextPlaceholders()); |
| }); |
| |
| it('should clear form state when current change is canceled and form state is pristine', () => { |
| const spy: any = spyOn(service as any, '_clear').and.callThrough(); |
| service.currentFormState = { isPristine: true } as any; |
| service.cancel(); |
| expect(spy).toHaveBeenCalled(); |
| }); |
| |
| it('should open modal when current change is canceled and form state is not pristine', () => { |
| spyOn(service['_modalService'], 'open').and.returnValue({ componentInstance: { title: '' }, result: { then: () => of(true) } } as any); |
| service.currentFormState = { isPristine: false } as any; |
| service.cancel(); |
| expect(modalService.open).toHaveBeenCalled(); |
| }); |
| |
| it('should call dispatch for saving an distribution group if a valid form state is provided', () => { |
| const spy: any = spyOn(service as any, '_clear').and.callThrough(); |
| service.currentFormState = { isValid: true } as any; |
| service.saveDistributionGroup(); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| expect(spy).toHaveBeenCalled(); |
| }); |
| |
| it('should displayNotification if a invalid form state is provided', () => { |
| const spy: any = spyOn(service['_utilService'], 'displayNotification').and.callThrough(); |
| service.currentFormState = { isValid: false } as any; |
| service.saveDistributionGroup(); |
| expect(spy).toHaveBeenCalled(); |
| }); |
| |
| it('should open modal before distribution group is deleted', () => { |
| spyOn(service['_modalService'], 'open').and.returnValue({ componentInstance: { title: '' }, result: { then: () => of(true) } } as any); |
| service.deleteDistributionGroup('x'); |
| expect(modalService.open).toHaveBeenCalled(); |
| }); |
| |
| it('should open modal before distribution group member is deleted', () => { |
| spyOn(service['_modalService'], 'open').and.returnValue({ componentInstance: { title: '' }, result: { then: () => of(true) } } as any); |
| service.deleteDistributionGroupMember('x', 'y'); |
| expect(modalService.open).toHaveBeenCalled(); |
| }); |
| |
| it('should dispatch an action via showMembersToSelectedGroup()', () => { |
| service.displayForm = false; |
| service.showMembersToSelectedGroup({ data: { id: 'x' } } as any); |
| expect(appState.dispatch).toHaveBeenCalled(); |
| }); |
| |
| it('should set displayDistributionGroupMember false if cancelMembersDisplay() was called', () => { |
| service.displayDistributionGroupMember = true; |
| service.cancelMembersDisplay(); |
| expect(service.displayDistributionGroupMember).toBeFalsy(); |
| }); |
| |
| it('should dispatch loadContacts action via loadContacts()', () => { |
| service.loadContacts('x'); |
| expect(appState.dispatch).toHaveBeenCalledWith(distributionGroupActions.loadContacts({ searchText: 'x' })); |
| }); |
| |
| it('should call loadContacts() via searchForContacts', () => { |
| const spy: any = spyOn(service, 'loadContacts'); |
| service.searchForContacts(of('xx')); |
| expect(spy).toHaveBeenCalled; |
| }); |
| |
| it('should return formatted argument', () => { |
| expect(service.formatter('x')).toBe('x'); |
| let contact: Contact = new Contact({ name: 'test', contactSearchString: 'y' }); |
| expect(service.formatter(contact)).toBe(contact.contactSearchString); |
| }); |
| |
| it('should displayNotification when member already assigned', () => { |
| const spy: any = spyOn(service['_utilService'], 'displayNotification'); |
| service['_distributionGroupMembers'] = [new DistributionGroupMember({ contactId: 'x' })]; |
| service['_selectedContact'] = new Contact({ uuid: 'x' }); |
| service.assignContactToGroup(); |
| expect(spy).toHaveBeenCalled; |
| }); |
| |
| it('should displayNotification when no contact was selected', () => { |
| const spy: any = spyOn(service['_utilService'], 'displayNotification'); |
| service['_selectedContact'] = undefined; |
| service.assignContactToGroup(); |
| expect(spy).toHaveBeenCalled; |
| }); |
| |
| it('should setSelectedContact()', () => { |
| let test = new Contact(); |
| service.setSelectedContact(test); |
| expect(service['_selectedContact']).toEqual(test); |
| }); |
| |
| it('should _clear form via clear()', () => { |
| service['_clear'](); |
| expect(service.displayForm).toBe(false); |
| }); |
| }); |