|
| 1 | +import { provideHttpClient } from '@angular/common/http'; |
| 2 | +import { provideHttpClientTesting } from '@angular/common/http/testing'; |
| 3 | +import { ApplicationRef } from '@angular/core'; |
| 4 | +import { TestBed } from '@angular/core/testing'; |
| 5 | +import { of, throwError } from 'rxjs'; |
| 6 | + |
| 7 | +import { Todo } from '../models/todo'; |
| 8 | +import { TodoService } from '../services/todo.service'; |
| 9 | +import { TodoStore } from './todo.store'; |
| 10 | + |
| 11 | +const mockTodos: Todo[] = [ |
| 12 | + { |
| 13 | + id: '1', |
| 14 | + title: 'Buy groceries', |
| 15 | + description: 'Milk, eggs, bread', |
| 16 | + completed: false, |
| 17 | + createdAt: '2024-01-01T00:00:00Z', |
| 18 | + }, |
| 19 | + { |
| 20 | + id: '2', |
| 21 | + title: 'Write tests', |
| 22 | + description: 'Cover the todo store', |
| 23 | + completed: true, |
| 24 | + createdAt: '2024-01-02T00:00:00Z', |
| 25 | + }, |
| 26 | +]; |
| 27 | + |
| 28 | +describe('TodoStore', () => { |
| 29 | + let store: TodoStore; |
| 30 | + let service: TodoService; |
| 31 | + let appRef: ApplicationRef; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + TestBed.configureTestingModule({ |
| 35 | + providers: [TodoStore, provideHttpClient(), provideHttpClientTesting()], |
| 36 | + }); |
| 37 | + |
| 38 | + store = TestBed.inject(TodoStore); |
| 39 | + service = TestBed.inject(TodoService); |
| 40 | + appRef = TestBed.inject(ApplicationRef); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should create', () => { |
| 44 | + expect(store).toBeTruthy(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should have syncEnabled true after onInit hook', () => { |
| 48 | + expect(store.syncEnabled()).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('loading todos', () => { |
| 52 | + it('should load todos when sync is enabled', async () => { |
| 53 | + vi.spyOn(service, 'getAll').mockReturnValue(of(mockTodos)); |
| 54 | + store.enableSync(); |
| 55 | + await appRef.whenStable(); |
| 56 | + |
| 57 | + expect(store.todos.value()).toEqual(mockTodos); |
| 58 | + expect(store.todos.isLoading()).toBe(false); |
| 59 | + expect(store.todos.error()).toBeFalsy(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should capture loading error', async () => { |
| 63 | + const error = new Error('network error'); |
| 64 | + vi.spyOn(service, 'getAll').mockReturnValue(throwError(() => error)); |
| 65 | + store.enableSync(); |
| 66 | + await appRef.whenStable(); |
| 67 | + |
| 68 | + expect(store.todos.error()).toBeTruthy(); |
| 69 | + expect(store.todos.isLoading()).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return empty list when sync is disabled', async () => { |
| 73 | + vi.spyOn(service, 'getAll').mockReturnValue(of(mockTodos)); |
| 74 | + store.disableSync(); |
| 75 | + await appRef.whenStable(); |
| 76 | + |
| 77 | + expect(store.todos.value()).toEqual([]); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('create', () => { |
| 82 | + it('should call service.create and reload', async () => { |
| 83 | + vi.spyOn(service, 'getAll').mockReturnValue(of(mockTodos)); |
| 84 | + const newTodo: Todo = { |
| 85 | + id: '3', |
| 86 | + title: 'New task', |
| 87 | + description: '', |
| 88 | + completed: false, |
| 89 | + createdAt: '2024-01-03T00:00:00Z', |
| 90 | + }; |
| 91 | + const createSpy = vi |
| 92 | + .spyOn(service, 'create') |
| 93 | + .mockReturnValue(of(newTodo)); |
| 94 | + |
| 95 | + store.create({ title: 'New task', description: '', completed: false }); |
| 96 | + await appRef.whenStable(); |
| 97 | + |
| 98 | + expect(createSpy).toHaveBeenCalledWith({ |
| 99 | + title: 'New task', |
| 100 | + description: '', |
| 101 | + completed: false, |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('update', () => { |
| 107 | + it('should call service.update and reload', async () => { |
| 108 | + vi.spyOn(service, 'getAll').mockReturnValue(of(mockTodos)); |
| 109 | + const updateSpy = vi |
| 110 | + .spyOn(service, 'update') |
| 111 | + .mockReturnValue(of({ ...mockTodos[0], title: 'Updated' })); |
| 112 | + |
| 113 | + store.update({ id: '1', changes: { title: 'Updated' } }); |
| 114 | + await appRef.whenStable(); |
| 115 | + |
| 116 | + expect(updateSpy).toHaveBeenCalledWith('1', { title: 'Updated' }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('remove', () => { |
| 121 | + it('should call service.remove and reload', async () => { |
| 122 | + vi.spyOn(service, 'getAll').mockReturnValue(of(mockTodos)); |
| 123 | + const removeSpy = vi |
| 124 | + .spyOn(service, 'remove') |
| 125 | + .mockReturnValue(of(undefined)); |
| 126 | + |
| 127 | + store.remove('1'); |
| 128 | + await appRef.whenStable(); |
| 129 | + |
| 130 | + expect(removeSpy).toHaveBeenCalledWith('1'); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('toggle', () => { |
| 135 | + it('should call service.update with flipped completed and reload', async () => { |
| 136 | + vi.spyOn(service, 'getAll').mockReturnValue(of(mockTodos)); |
| 137 | + const updateSpy = vi |
| 138 | + .spyOn(service, 'update') |
| 139 | + .mockReturnValue(of({ ...mockTodos[0], completed: true })); |
| 140 | + |
| 141 | + store.toggle(mockTodos[0]); |
| 142 | + await appRef.whenStable(); |
| 143 | + |
| 144 | + expect(updateSpy).toHaveBeenCalledWith('1', { completed: true }); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments