|
| 1 | +# Karma Unit Testing - Quick Start Guide |
| 2 | + |
| 3 | +This guide will help you quickly get started with unit testing in the eForm Angular Frontend project. |
| 4 | + |
| 5 | +## Quick Commands |
| 6 | + |
| 7 | +```bash |
| 8 | +# Run all tests |
| 9 | +cd eform-client |
| 10 | +ng test |
| 11 | + |
| 12 | +# Run tests in headless mode (for CI/CD) |
| 13 | +ng test --watch=false --browsers=ChromeHeadless |
| 14 | + |
| 15 | +# Run tests with code coverage |
| 16 | +ng test --code-coverage --watch=false --browsers=ChromeHeadless |
| 17 | + |
| 18 | +# Generate a new spec file template |
| 19 | +./generate-spec.sh src/app/modules/path/to/component.component.ts |
| 20 | +``` |
| 21 | + |
| 22 | +## Files to Reference |
| 23 | + |
| 24 | +### Documentation |
| 25 | +- **[TESTING.md](./TESTING.md)** - Complete testing guide with patterns and examples |
| 26 | +- **[TESTING-SUMMARY.md](./TESTING-SUMMARY.md)** - Implementation summary and status |
| 27 | + |
| 28 | +### Example Spec Files |
| 29 | + |
| 30 | +**Simple Components (good starting point)**: |
| 31 | +- `src/app/modules/advanced/components/workers/worker-delete/worker-delete.component.spec.ts` |
| 32 | +- `src/app/modules/advanced/components/folders/folder-delete/folder-delete.component.spec.ts` |
| 33 | +- `src/app/modules/advanced/components/units/units-otp-code/units-otp-code.component.spec.ts` |
| 34 | + |
| 35 | +**Complex Components (with dialogs and multiple methods)**: |
| 36 | +- `src/app/modules/advanced/components/units/units.component.spec.ts` |
| 37 | +- `src/app/modules/advanced/components/workers/workers/workers.component.spec.ts` |
| 38 | +- `src/app/modules/advanced/components/folders/folders/folders.component.spec.ts` |
| 39 | + |
| 40 | +**Form Components (create/edit)**: |
| 41 | +- `src/app/modules/advanced/components/units/unit-create/unit-create.component.spec.ts` |
| 42 | +- `src/app/modules/advanced/components/workers/worker-edit-create/worker-edit-create.component.spec.ts` |
| 43 | + |
| 44 | +## Basic Test Structure |
| 45 | + |
| 46 | +```typescript |
| 47 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 48 | +import { YourComponent } from './your.component'; |
| 49 | +import { YourService } from 'src/app/common/services'; |
| 50 | +import { of } from 'rxjs'; |
| 51 | + |
| 52 | +describe('YourComponent', () => { |
| 53 | + let component: YourComponent; |
| 54 | + let fixture: ComponentFixture<YourComponent>; |
| 55 | + let mockService: jasmine.SpyObj<YourService>; |
| 56 | + |
| 57 | + beforeEach(waitForAsync(() => { |
| 58 | + mockService = jasmine.createSpyObj('YourService', ['getData']); |
| 59 | + |
| 60 | + TestBed.configureTestingModule({ |
| 61 | + declarations: [YourComponent], |
| 62 | + providers: [ |
| 63 | + { provide: YourService, useValue: mockService } |
| 64 | + ] |
| 65 | + }).compileComponents(); |
| 66 | + })); |
| 67 | + |
| 68 | + beforeEach(() => { |
| 69 | + fixture = TestBed.createComponent(YourComponent); |
| 70 | + component = fixture.componentInstance; |
| 71 | + }); |
| 72 | + |
| 73 | + it('should create', () => { |
| 74 | + expect(component).toBeTruthy(); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('getData', () => { |
| 78 | + it('should load data successfully', () => { |
| 79 | + const mockData = { success: true, message: '', model: [] }; |
| 80 | + mockService.getData.and.returnValue(of(mockData)); |
| 81 | + |
| 82 | + component.getData(); |
| 83 | + |
| 84 | + expect(mockService.getData).toHaveBeenCalled(); |
| 85 | + expect(component.data).toEqual(mockData.model); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +## Common Mocking Patterns |
| 92 | + |
| 93 | +### Service |
| 94 | +```typescript |
| 95 | +mockService = jasmine.createSpyObj('ServiceName', ['method']); |
| 96 | +mockService.method.and.returnValue(of({ success: true, message: '', model: [] })); |
| 97 | +``` |
| 98 | + |
| 99 | +### Dialog |
| 100 | +```typescript |
| 101 | +mockDialog = jasmine.createSpyObj('MatDialog', ['open']); |
| 102 | +mockDialog.open.and.returnValue({ afterClosed: () => of(true) } as any); |
| 103 | +``` |
| 104 | + |
| 105 | +### Store |
| 106 | +```typescript |
| 107 | +mockStore = jasmine.createSpyObj('Store', ['select', 'dispatch']); |
| 108 | +mockStore.select.and.returnValue(of(true)); |
| 109 | +``` |
| 110 | + |
| 111 | +### TranslateService |
| 112 | +```typescript |
| 113 | +mockTranslate = jasmine.createSpyObj('TranslateService', ['stream']); |
| 114 | +mockTranslate.stream.and.returnValue(of('Translation')); |
| 115 | +``` |
| 116 | + |
| 117 | +## Testing Checklist |
| 118 | + |
| 119 | +For each component, ensure: |
| 120 | +- [ ] Component creation test exists |
| 121 | +- [ ] All public methods have tests |
| 122 | +- [ ] Success scenarios are tested |
| 123 | +- [ ] Failure scenarios are tested |
| 124 | +- [ ] Edge cases are tested (null, empty, etc.) |
| 125 | +- [ ] Service method calls are verified |
| 126 | +- [ ] Component state changes are verified |
| 127 | +- [ ] Dialog interactions are tested (if applicable) |
| 128 | + |
| 129 | +## Status |
| 130 | + |
| 131 | +**Completed**: 8 spec files covering units, workers, and folders components |
| 132 | +**Remaining**: ~90 components across various modules |
| 133 | +**Infrastructure**: ✅ Complete and ready to use |
| 134 | + |
| 135 | +## Need Help? |
| 136 | + |
| 137 | +1. Read [TESTING.md](./TESTING.md) for detailed patterns |
| 138 | +2. Look at example spec files listed above |
| 139 | +3. Use `generate-spec.sh` to create templates |
| 140 | +4. Follow the established patterns |
| 141 | + |
| 142 | +## CI/CD |
| 143 | + |
| 144 | +Tests can be run in CI/CD pipelines with: |
| 145 | +```bash |
| 146 | +ng test --watch=false --browsers=ChromeHeadless --code-coverage |
| 147 | +``` |
| 148 | + |
| 149 | +Karma is configured with appropriate timeouts for CI/CD environments. |
0 commit comments