|
1 | 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | | - |
| 2 | +import { MockStore, provideMockStore } from '@ngrx/store/testing'; |
| 3 | +import { FormBuilder } from '@angular/forms'; |
| 4 | +import { Subscription } from 'rxjs'; |
3 | 5 | import { CreateProjectComponent } from './create-project.component'; |
| 6 | +import { ProjectState } from '../store/project.reducer'; |
| 7 | +import { Project } from 'src/app/modules/shared/models'; |
| 8 | +import { getProjectToEdit } from '../store/project.selectors'; |
| 9 | +import { UpdateProject, CreateProject, ResetProjectToEdit } from '../store/project.actions'; |
4 | 10 |
|
5 | 11 | describe('InputProjectComponent', () => { |
6 | 12 | let component: CreateProjectComponent; |
7 | 13 | let fixture: ComponentFixture<CreateProjectComponent>; |
| 14 | + let store: MockStore<ProjectState>; |
| 15 | + let getProjectToEditMock; |
| 16 | + |
| 17 | + const state = { |
| 18 | + projectList: [{ id: '', name: '', project_type_id: '' }], |
| 19 | + isLoading: false, |
| 20 | + message: '', |
| 21 | + projectToEdit: undefined, |
| 22 | + }; |
| 23 | + |
| 24 | + const project: Project = { |
| 25 | + id: '1', |
| 26 | + name: 'Test', |
| 27 | + description: 'xxx', |
| 28 | + project_type_id: '123', |
| 29 | + }; |
| 30 | + |
| 31 | + const projectForm = { |
| 32 | + name: 'Test', |
| 33 | + description: 'xxx', |
| 34 | + project_type_id: '123', |
| 35 | + }; |
8 | 36 |
|
9 | 37 | beforeEach(async(() => { |
10 | 38 | TestBed.configureTestingModule({ |
11 | 39 | declarations: [CreateProjectComponent], |
| 40 | + providers: [FormBuilder, provideMockStore({ initialState: state })], |
12 | 41 | }).compileComponents(); |
13 | 42 | })); |
14 | 43 |
|
15 | 44 | beforeEach(() => { |
16 | 45 | fixture = TestBed.createComponent(CreateProjectComponent); |
17 | 46 | component = fixture.componentInstance; |
18 | 47 | fixture.detectChanges(); |
| 48 | + |
| 49 | + store = TestBed.inject(MockStore); |
| 50 | + store.setState(state); |
| 51 | + |
| 52 | + component.projectToEditSubscription = new Subscription(); |
| 53 | + component.projectTypesSubscription = new Subscription(); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + fixture.destroy(); |
19 | 58 | }); |
20 | 59 |
|
21 | 60 | it('component should be created', () => { |
22 | 61 | expect(component).toBeTruthy(); |
23 | 62 | }); |
| 63 | + |
| 64 | + it('should destroy the subscriptions', () => { |
| 65 | + component.projectToEditSubscription = new Subscription(); |
| 66 | + component.projectTypesSubscription = new Subscription(); |
| 67 | + const subscription = spyOn(component.projectToEditSubscription, 'unsubscribe'); |
| 68 | + const projectTypeSubscription = spyOn(component.projectTypesSubscription, 'unsubscribe'); |
| 69 | + |
| 70 | + component.ngOnDestroy(); |
| 71 | + |
| 72 | + expect(subscription).toHaveBeenCalledTimes(1); |
| 73 | + expect(projectTypeSubscription).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should reset form #onSubmit and dispatch UpdateProject action', () => { |
| 77 | + const currentState = { |
| 78 | + data: [project], |
| 79 | + isLoading: false, |
| 80 | + message: '', |
| 81 | + projectToEdit: project, |
| 82 | + }; |
| 83 | + |
| 84 | + getProjectToEditMock = store.overrideSelector(getProjectToEdit, currentState.projectToEdit); |
| 85 | + component.projectToEdit = getProjectToEditMock; |
| 86 | + |
| 87 | + const projectUpdated = { |
| 88 | + id: component.projectToEdit.id, |
| 89 | + name: 'Test', |
| 90 | + description: 'xxx', |
| 91 | + project_type_id: '123', |
| 92 | + }; |
| 93 | + |
| 94 | + component.projectToEditSubscription = new Subscription(); |
| 95 | + component.projectTypesSubscription = new Subscription(); |
| 96 | + spyOn(component.projectForm, 'reset'); |
| 97 | + spyOn(store, 'dispatch'); |
| 98 | + const subscription = spyOn(component.projectToEditSubscription, 'unsubscribe'); |
| 99 | + const projectTypeSubscription = spyOn(component.projectTypesSubscription, 'unsubscribe'); |
| 100 | + |
| 101 | + component.onSubmit(projectForm); |
| 102 | + component.ngOnDestroy(); |
| 103 | + |
| 104 | + expect(subscription).toHaveBeenCalledTimes(1); |
| 105 | + expect(projectTypeSubscription).toHaveBeenCalledTimes(1); |
| 106 | + |
| 107 | + expect(component.projectForm.reset).toHaveBeenCalled(); |
| 108 | + expect(store.dispatch).toHaveBeenCalledTimes(1); |
| 109 | + expect(store.dispatch).toHaveBeenCalledWith(new UpdateProject(projectUpdated)); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should reset form onSubmit and dispatch CreateProject action', () => { |
| 113 | + component.projectToEdit = undefined; |
| 114 | + |
| 115 | + spyOn(component.projectForm, 'reset'); |
| 116 | + spyOn(store, 'dispatch'); |
| 117 | + |
| 118 | + component.onSubmit(project); |
| 119 | + |
| 120 | + expect(component.projectForm.reset).toHaveBeenCalled(); |
| 121 | + expect(store.dispatch).toHaveBeenCalledTimes(1); |
| 122 | + expect(store.dispatch).toHaveBeenCalledWith(new CreateProject(project)); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should set data in projectForm', () => { |
| 126 | + component.projectToEditSubscription = new Subscription(); |
| 127 | + component.projectTypesSubscription = new Subscription(); |
| 128 | + component.projectToEdit = project; |
| 129 | + |
| 130 | + const subscription = spyOn(component.projectToEditSubscription, 'unsubscribe'); |
| 131 | + const projectTypeSubscription = spyOn(component.projectTypesSubscription, 'unsubscribe'); |
| 132 | + spyOn(component.projectForm, 'setValue'); |
| 133 | + |
| 134 | + component.setDataToUpdate(project); |
| 135 | + component.ngOnDestroy(); |
| 136 | + |
| 137 | + expect(subscription).toHaveBeenCalledTimes(1); |
| 138 | + expect(projectTypeSubscription).toHaveBeenCalledTimes(1); |
| 139 | + expect(component.projectForm.setValue).toHaveBeenCalledTimes(1); |
| 140 | + expect(component.projectForm.setValue).toHaveBeenCalledWith(projectForm); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should dispatch a ResetActivityToEdit action', () => { |
| 144 | + spyOn(store, 'dispatch'); |
| 145 | + |
| 146 | + component.cancelButton(); |
| 147 | + |
| 148 | + expect(store.dispatch).toHaveBeenCalledTimes(1); |
| 149 | + expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectToEdit()); |
| 150 | + }); |
24 | 151 | }); |
0 commit comments