|
| 1 | +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { provideMockStore, MockStore } from '@ngrx/store/testing'; |
| 3 | +import { FormsModule, ReactiveFormsModule } from '@angular/forms'; |
| 4 | + |
| 5 | +import { TechnologyState } from '../../../shared/store/technology.reducers'; |
| 6 | +import { allTechnologies } from '../../../shared/store/technology.selectors'; |
| 7 | +import { EntryFieldsComponent } from './entry-fields.component'; |
| 8 | +import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer'; |
| 9 | +import { allProjects } from '../../../customer-management/components/projects/components/store/project.selectors'; |
| 10 | +import { allEntries } from '../../store/entry.selectors'; |
| 11 | +import * as actions from '../../../shared/store/technology.actions'; |
| 12 | +import * as entryActions from '../../store/entry.actions'; |
| 13 | + |
| 14 | +describe('EntryFieldsComponent', () => { |
| 15 | + type Merged = TechnologyState & ProjectState; |
| 16 | + let component: EntryFieldsComponent; |
| 17 | + let fixture: ComponentFixture<EntryFieldsComponent>; |
| 18 | + let store: MockStore<Merged>; |
| 19 | + let mockTechnologySelector; |
| 20 | + let mockProjectsSelector; |
| 21 | + let mockEntrySelector; |
| 22 | + let length; |
| 23 | + |
| 24 | + const state = { |
| 25 | + projects: { |
| 26 | + projectList: [{ id: 'id', name: 'name', description: 'description', project_type_id: '123' }], |
| 27 | + isLoading: false, |
| 28 | + message: '', |
| 29 | + projectToEdit: undefined, |
| 30 | + }, |
| 31 | + technologies: { |
| 32 | + technologyList: { items: [{ name: 'java' }] }, |
| 33 | + isLoading: false, |
| 34 | + }, |
| 35 | + activities: { |
| 36 | + data: [{ id: 'fc5fab41-a21e-4155-9d05-511b956ebd05', tenant_id: 'ioet', deleted: null, name: 'Training 2' }], |
| 37 | + isLoading: false, |
| 38 | + message: 'Data fetch successfully!', |
| 39 | + activityIdToEdit: '', |
| 40 | + }, |
| 41 | + entries: { |
| 42 | + active: { |
| 43 | + id: 'id-15', |
| 44 | + project_id: 'project-id-15', |
| 45 | + description: 'description for active entry', |
| 46 | + technologies: ['java', 'typescript'], |
| 47 | + }, |
| 48 | + entryList: [], |
| 49 | + isLoading: false, |
| 50 | + message: '', |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + const entry = { |
| 55 | + id: 'id-15', |
| 56 | + project_id: 'project-id-15', |
| 57 | + description: 'description for active entry', |
| 58 | + }; |
| 59 | + |
| 60 | + beforeEach(async(() => { |
| 61 | + TestBed.configureTestingModule({ |
| 62 | + declarations: [EntryFieldsComponent], |
| 63 | + providers: [provideMockStore({ initialState: state })], |
| 64 | + imports: [FormsModule, ReactiveFormsModule], |
| 65 | + }).compileComponents(); |
| 66 | + store = TestBed.inject(MockStore); |
| 67 | + mockTechnologySelector = store.overrideSelector(allTechnologies, state.technologies); |
| 68 | + mockProjectsSelector = store.overrideSelector(allProjects, state.projects); |
| 69 | + mockEntrySelector = store.overrideSelector(allEntries, state.entries); |
| 70 | + })); |
| 71 | + |
| 72 | + beforeEach(() => { |
| 73 | + fixture = TestBed.createComponent(EntryFieldsComponent); |
| 74 | + component = fixture.componentInstance; |
| 75 | + fixture.detectChanges(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should create', () => { |
| 79 | + expect(component).toBeTruthy(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should set data in entryForm', () => { |
| 83 | + const entryDataForm = { |
| 84 | + description: 'description for active entry', |
| 85 | + technologies: null, |
| 86 | + }; |
| 87 | + |
| 88 | + spyOn(component.entryForm, 'patchValue'); |
| 89 | + |
| 90 | + component.setDataToUpdate(entry); |
| 91 | + |
| 92 | + expect(component.entryForm.patchValue).toHaveBeenCalledTimes(1); |
| 93 | + expect(component.entryForm.patchValue).toHaveBeenCalledWith({ description: entryDataForm.description }); |
| 94 | + expect(component.selectedTechnology).toEqual([]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should dispatch FindTechnology action #getTechnologies', () => { |
| 98 | + const value = 'java'; |
| 99 | + spyOn(store, 'dispatch'); |
| 100 | + length = value.length; |
| 101 | + component.getTechnologies(value); |
| 102 | + |
| 103 | + expect(component.showlist).toBe(true); |
| 104 | + expect(store.dispatch).toHaveBeenCalledWith(new actions.FindTechnology(value)); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should NOT dispatch FindTechnology action #getTechnologies', () => { |
| 108 | + const value = 'j'; |
| 109 | + spyOn(store, 'dispatch'); |
| 110 | + length = value.length; |
| 111 | + component.getTechnologies(value); |
| 112 | + |
| 113 | + expect(store.dispatch).not.toHaveBeenCalledWith(new actions.FindTechnology(value)); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should add a new tag #setTechnology', () => { |
| 117 | + spyOn(store, 'dispatch'); |
| 118 | + const name = 'ngrx'; |
| 119 | + component.selectedTechnology = ['java', 'javascript']; |
| 120 | + component.selectedTechnology.indexOf(name); |
| 121 | + length = component.selectedTechnology.length; |
| 122 | + |
| 123 | + const newEntry = { |
| 124 | + id: 'id-15', |
| 125 | + project_id: 'project-id-15', |
| 126 | + }; |
| 127 | + |
| 128 | + component.setTechnology(name); |
| 129 | + expect(component.selectedTechnology.length).toBe(3); |
| 130 | + expect(store.dispatch).toHaveBeenCalledWith( |
| 131 | + new entryActions.UpdateActiveEntry({ ...newEntry, technologies: component.selectedTechnology }) |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should NOT add a new tag #setTechnology', () => { |
| 136 | + const name = 'ngrx'; |
| 137 | + component.selectedTechnology = [ |
| 138 | + 'java', |
| 139 | + 'javascript', |
| 140 | + 'angular', |
| 141 | + 'angular-ui', |
| 142 | + 'typescript', |
| 143 | + 'scss', |
| 144 | + 'bootstrap', |
| 145 | + 'jasmine', |
| 146 | + 'karme', |
| 147 | + 'github', |
| 148 | + ]; |
| 149 | + component.selectedTechnology.indexOf(name); |
| 150 | + length = component.selectedTechnology.length; |
| 151 | + component.setTechnology(name); |
| 152 | + expect(component.selectedTechnology.length).toBe(10); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should call the removeTag function #setTechnology', () => { |
| 156 | + const name = 'java'; |
| 157 | + component.selectedTechnology = ['java', 'javascript']; |
| 158 | + const index = component.selectedTechnology.indexOf(name); |
| 159 | + spyOn(component, 'removeTag'); |
| 160 | + component.setTechnology(name); |
| 161 | + expect(component.removeTag).toHaveBeenCalledWith(index); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should dispatch UpdateActiveEntry action #removeTag function', () => { |
| 165 | + spyOn(store, 'dispatch'); |
| 166 | + const index = 1; |
| 167 | + const newEntry = { |
| 168 | + id: 'id-15', |
| 169 | + project_id: 'project-id-15', |
| 170 | + }; |
| 171 | + component.selectedTechnology = ['java', 'angular']; |
| 172 | + const technologies = component.selectedTechnology.filter((item) => item !== component.selectedTechnology[index]); |
| 173 | + component.removeTag(index); |
| 174 | + expect(store.dispatch).toHaveBeenCalledWith(new entryActions.UpdateActiveEntry({ ...newEntry, technologies })); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should dispatch UpdateActiveEntry action #onSubmit', () => { |
| 178 | + spyOn(store, 'dispatch'); |
| 179 | + component.onSubmit(); |
| 180 | + expect(store.dispatch).toHaveBeenCalledWith(new entryActions.UpdateActiveEntry(entry)); |
| 181 | + }); |
| 182 | +}); |
0 commit comments