Skip to content

Commit 78b546e

Browse files
author
wgd3
committed
updated tests with new mock factories
1 parent 2447443 commit 78b546e

18 files changed

Lines changed: 141 additions & 84 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
## [1.0.1](https://github.com/wgd3/full-stack-todo/compare/v1.0.0...v1.0.1) (2023-03-21)
22

3-
43
### Other
54

6-
* added codecov token to main CI workflow ([fe6022d](https://github.com/wgd3/full-stack-todo/commit/fe6022d545899ba80c666b1dec131ebb60a81f87))
7-
* added e2e tests to main CI workflow ([e4dfd92](https://github.com/wgd3/full-stack-todo/commit/e4dfd92c23c432860c83a03b06c780dec8323aa7))
8-
* added part 7 ([206935e](https://github.com/wgd3/full-stack-todo/commit/206935edfb74cc119136eb5f70295daf2aa5bd70))
9-
* attempting to fix formatting commands ([4019b34](https://github.com/wgd3/full-stack-todo/commit/4019b3471af4742c6c2f8da81a5635b5020c45db))
5+
- added codecov token to main CI workflow ([fe6022d](https://github.com/wgd3/full-stack-todo/commit/fe6022d545899ba80c666b1dec131ebb60a81f87))
6+
- added e2e tests to main CI workflow ([e4dfd92](https://github.com/wgd3/full-stack-todo/commit/e4dfd92c23c432860c83a03b06c780dec8323aa7))
7+
- added part 7 ([206935e](https://github.com/wgd3/full-stack-todo/commit/206935edfb74cc119136eb5f70295daf2aa5bd70))
8+
- attempting to fix formatting commands ([4019b34](https://github.com/wgd3/full-stack-todo/commit/4019b3471af4742c6c2f8da81a5635b5020c45db))
109

1110
## 1.0.0 (2023-03-20)
1211

apps/server-e2e/src/server/todo-controller.spec.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,17 @@ import {
33
ServerFeatureTodoController,
44
ServerFeatureTodoService,
55
} from '@fst/server/feature-todo';
6+
import { MockType, repositoryMockFactory } from '@fst/server/util';
67
import { ITodo } from '@fst/shared/domain';
7-
import { createMockTodo } from '@fst/shared/util-testing';
8+
import { createMockTodo, createMockUser } from '@fst/shared/util-testing';
89
import { HttpStatus, INestApplication, ValidationPipe } from '@nestjs/common';
910
import { APP_PIPE } from '@nestjs/core';
1011
import { Test } from '@nestjs/testing';
1112
import { getRepositoryToken } from '@nestjs/typeorm';
1213
import * as request from 'supertest';
1314
import { Repository } from 'typeorm';
1415

15-
export type MockType<T> = {
16-
[P in keyof T]?: jest.Mock<unknown>;
17-
};
18-
19-
export const repositoryMockFactory: () => MockType<Repository<any>> = jest.fn(
20-
() => ({
21-
findOne: jest.fn((entity) => entity),
22-
findOneBy: jest.fn(() => ({})),
23-
save: jest.fn((entity) => entity),
24-
findOneOrFail: jest.fn(() => ({})),
25-
delete: jest.fn(() => null),
26-
find: jest.fn((entities) => entities),
27-
})
28-
);
16+
const mockUser = createMockUser();
2917

3018
describe('ServerFeatureTodoController E2E', () => {
3119
const todoUrl = `/todos`;
@@ -70,7 +58,7 @@ describe('ServerFeatureTodoController E2E', () => {
7058

7159
describe('POST /todos', () => {
7260
it('should create a todo item', () => {
73-
const { id, completed, title, description } = createMockTodo();
61+
const { id, completed, title, description } = createMockTodo(mockUser.id);
7462
jest
7563
.spyOn(repoMock, 'save')
7664
.mockReturnValue(
@@ -91,7 +79,7 @@ describe('ServerFeatureTodoController E2E', () => {
9179
});
9280

9381
it('should prevent adding a to-do with an ID', () => {
94-
const { id, title, description } = createMockTodo();
82+
const { id, title, description } = createMockTodo(mockUser.id);
9583
return request
9684
.default(app.getHttpServer())
9785
.post(todoUrl)
@@ -108,7 +96,7 @@ describe('ServerFeatureTodoController E2E', () => {
10896
});
10997

11098
xit('should prevent adding a to-do with an existing title', () => {
111-
const todo = createMockTodo();
99+
const todo = createMockTodo(mockUser.id);
112100
return request
113101
.default(app.getHttpServer())
114102
.post(todoUrl)
@@ -117,7 +105,7 @@ describe('ServerFeatureTodoController E2E', () => {
117105
});
118106

119107
it('should prevent adding a todo item with a completed status', () => {
120-
const { id, ...todo } = createMockTodo();
108+
const { id, ...todo } = createMockTodo(mockUser.id);
121109
return request
122110
.default(app.getHttpServer())
123111
.post(todoUrl)

libs/client/data-access/src/lib/api.service.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { HttpClient } from '@angular/common/http';
22
import { HttpClientTestingModule } from '@angular/common/http/testing';
33
import { TestBed } from '@angular/core/testing';
44
import { ITodo } from '@fst/shared/domain';
5-
import { createMockTodo } from '@fst/shared/util-testing';
5+
import { createMockTodo, createMockUser } from '@fst/shared/util-testing';
66
import { of } from 'rxjs';
7-
import exp = require('constants');
87

98
import { ApiService } from './api.service';
109

10+
const mockUser = createMockUser();
11+
1112
describe('ApiService', () => {
1213
let service: ApiService;
1314
let http: HttpClient;
@@ -26,7 +27,7 @@ describe('ApiService', () => {
2627

2728
it('should get a list of to-do items', (done) => {
2829
const todos: ITodo[] = Array.from({ length: 5 }).map(() =>
29-
createMockTodo()
30+
createMockTodo(mockUser.id)
3031
);
3132
const httpSpy = jest.spyOn(http, 'get').mockReturnValue(of(todos));
3233
service.getAllToDoItems().subscribe({
@@ -41,7 +42,7 @@ describe('ApiService', () => {
4142
});
4243

4344
it('should get a single to-do item', (done) => {
44-
const todo = createMockTodo();
45+
const todo = createMockTodo(mockUser.id);
4546
const httpSpy = jest.spyOn(http, 'get').mockReturnValue(of(todo));
4647
service.getToDoById(todo.id).subscribe({
4748
next: (val) => {
@@ -54,7 +55,7 @@ describe('ApiService', () => {
5455
});
5556

5657
it('should create a single to-do item', (done) => {
57-
const todo = createMockTodo();
58+
const todo = createMockTodo(mockUser.id);
5859
const httpSpy = jest.spyOn(http, 'post').mockReturnValue(of(todo));
5960
service.createToDo(todo).subscribe({
6061
next: (val) => {
@@ -68,7 +69,7 @@ describe('ApiService', () => {
6869
});
6970

7071
it('should update a single to-do item', (done) => {
71-
const todo = createMockTodo();
72+
const todo = createMockTodo(mockUser.id);
7273
const httpSpy = jest.spyOn(http, 'patch').mockReturnValue(of(todo));
7374
service.updateToDo(todo.id, todo).subscribe({
7475
next: (val) => {
@@ -82,7 +83,7 @@ describe('ApiService', () => {
8283
});
8384

8485
it('should update a single to-do item', (done) => {
85-
const todo = createMockTodo();
86+
const todo = createMockTodo(mockUser.id);
8687
const httpSpy = jest.spyOn(http, 'put').mockReturnValue(of(todo));
8788
service.createOrUpdateToDo(todo.id, todo).subscribe({
8889
next: (val) => {
@@ -96,7 +97,7 @@ describe('ApiService', () => {
9697
});
9798

9899
it('should delete a single to-do item', (done) => {
99-
const todo = createMockTodo();
100+
const todo = createMockTodo(mockUser.id);
100101
const httpSpy = jest.spyOn(http, 'delete').mockReturnValue(of(null));
101102
service.deleteToDo(todo.id).subscribe({
102103
next: (val) => {

libs/client/feature-dashboard/src/lib/feature-dashboard/feature-dashboard.component.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { HttpClientTestingModule } from '@angular/common/http/testing';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { ApiService } from '@fst/client/data-access';
4-
import { createMockTodo } from '@fst/shared/util-testing';
4+
import { createMockTodo, createMockUser } from '@fst/shared/util-testing';
55
import { of } from 'rxjs';
66

77
import { FeatureDashboardComponent } from './feature-dashboard.component';
88

9+
const mockUser = createMockUser();
10+
911
describe('FeatureDashboardComponent', () => {
1012
let component: FeatureDashboardComponent;
1113
let apiService: ApiService;
@@ -28,12 +30,14 @@ describe('FeatureDashboardComponent', () => {
2830
});
2931

3032
it('should return a unique id for each todo', () => {
31-
const todo = createMockTodo();
33+
const todo = createMockTodo(mockUser.id);
3234
expect(component.trackTodo(0, todo)).toBe(todo.id);
3335
});
3436

3537
it('should trigger a refresh of data', (done) => {
36-
const todos = Array.from({ length: 5 }).map(() => createMockTodo());
38+
const todos = Array.from({ length: 5 }).map(() =>
39+
createMockTodo(mockUser.id)
40+
);
3741
const spy = jest
3842
.spyOn(apiService, 'getAllToDoItems')
3943
.mockReturnValue(of(todos));
@@ -44,7 +48,7 @@ describe('FeatureDashboardComponent', () => {
4448
});
4549

4650
it('should be able to toggle the completion of a todo', (done) => {
47-
const todo = createMockTodo({ completed: false });
51+
const todo = createMockTodo(mockUser.id, { completed: false });
4852
const updateSpy = jest
4953
.spyOn(apiService, 'updateToDo')
5054
.mockReturnValue(of({ ...todo, completed: true }));
@@ -60,7 +64,9 @@ describe('FeatureDashboardComponent', () => {
6064
});
6165

6266
it('should be able to delete a todo', (done) => {
63-
const todos = Array.from({ length: 5 }).map(() => createMockTodo());
67+
const todos = Array.from({ length: 5 }).map(() =>
68+
createMockTodo(mockUser.id)
69+
);
6470
component.todos$.next(todos);
6571
const deleteSpy = jest
6672
.spyOn(apiService, 'deleteToDo')
@@ -76,7 +82,7 @@ describe('FeatureDashboardComponent', () => {
7682
});
7783

7884
it('should be able to toggle the completion of a todo', (done) => {
79-
const todo = createMockTodo({ completed: false });
85+
const todo = createMockTodo(mockUser.id, { completed: false });
8086
const updateSpy = jest
8187
.spyOn(apiService, 'updateToDo')
8288
.mockReturnValue(of({ ...todo, completed: true }));

libs/client/ui-components/src/lib/to-do/to-do.component.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
22
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
33
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
4-
import { createMockTodo } from '@fst/shared/util-testing';
4+
import { createMockTodo, createMockUser } from '@fst/shared/util-testing';
55
import { EditableModule } from '@ngneat/edit-in-place';
66

77
import { ToDoComponent } from './to-do.component';
88

9+
const mockUser = createMockUser();
10+
911
describe('ToDoComponent', () => {
1012
let component: ToDoComponent;
1113
let fixture: ComponentFixture<ToDoComponent>;
@@ -31,14 +33,14 @@ describe('ToDoComponent', () => {
3133
});
3234

3335
it('should init with data without issue', () => {
34-
const todo = createMockTodo();
36+
const todo = createMockTodo(mockUser.id);
3537
component.todo = todo;
3638
component.ngOnInit();
3739
expect(component.todoForm).toBeDefined();
3840
});
3941

4042
it('should save data', (done) => {
41-
const todo = createMockTodo();
43+
const todo = createMockTodo(mockUser.id);
4244
component.updateTodo.subscribe((data) => {
4345
expect(data).toStrictEqual(todo);
4446
done();
@@ -50,7 +52,7 @@ describe('ToDoComponent', () => {
5052
});
5153

5254
it('should cancel an edit', () => {
53-
const todo = createMockTodo();
55+
const todo = createMockTodo(mockUser.id);
5456
component.todo = todo;
5557
component.ngOnInit();
5658
component.todoForm.controls.title.setValue('foo');
@@ -59,7 +61,7 @@ describe('ToDoComponent', () => {
5961
});
6062

6163
it('should successfully toggle completion', (done) => {
62-
const todo = createMockTodo();
64+
const todo = createMockTodo(mockUser.id);
6365

6466
component.updateTodo.subscribe((data) => {
6567
expect(data).toStrictEqual({ ...todo, completed: !todo.completed });
@@ -77,7 +79,7 @@ describe('ToDoComponent', () => {
7779
});
7880

7981
it('should save data', (done) => {
80-
const todo = createMockTodo();
82+
const todo = createMockTodo(mockUser.id);
8183
component.deleteTodo.subscribe((data) => {
8284
expect(data).toStrictEqual(todo);
8385
done();

libs/server/feature-auth/src/lib/jwt-strategy.service.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { JwtStrategyService } from './jwt-strategy.service';
2+
import { JwtStrategy } from './jwt-strategy.service';
33

4-
describe('JwtStrategyService', () => {
5-
let service: JwtStrategyService;
4+
describe('JwtStrategy', () => {
5+
let service: JwtStrategy;
66

77
beforeEach(async () => {
88
const module: TestingModule = await Test.createTestingModule({
9-
providers: [JwtStrategyService],
9+
providers: [JwtStrategy],
1010
}).compile();
1111

12-
service = module.get<JwtStrategyService>(JwtStrategyService);
12+
service = module.get<JwtStrategy>(JwtStrategy);
1313
});
1414

1515
it('should be defined', () => {

libs/server/feature-todo/src/lib/server-feature-todo.controller.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { ToDoEntitySchema } from '@fst/server/data-access';
2-
import { createMockTodo } from '@fst/shared/util-testing';
2+
import { repositoryMockFactory } from '@fst/server/util';
3+
import { createMockTodo, createMockUser } from '@fst/shared/util-testing';
34
import { Test } from '@nestjs/testing';
45
import { getRepositoryToken } from '@nestjs/typeorm';
56
import { ServerFeatureTodoController } from './server-feature-todo.controller';
67
import { ServerFeatureTodoService } from './server-feature-todo.service';
7-
import { repositoryMockFactory } from './server-feature-todo.service.spec';
8+
9+
const mockUser = createMockUser();
810

911
describe('ServerFeatureTodoController', () => {
1012
let controller: ServerFeatureTodoController;
@@ -34,7 +36,9 @@ describe('ServerFeatureTodoController', () => {
3436
jest
3537
.spyOn(service, 'getAll')
3638
.mockReturnValue(
37-
Promise.resolve(Array.from({ length: 5 }).map(() => createMockTodo()))
39+
Promise.resolve(
40+
Array.from({ length: 5 }).map(() => createMockTodo(mockUser.id))
41+
)
3842
);
3943

4044
const res = await controller.getAll();
@@ -43,27 +47,27 @@ describe('ServerFeatureTodoController', () => {
4347
});
4448

4549
it('should return a single todo by ID', async () => {
46-
const todo = createMockTodo();
50+
const todo = createMockTodo(mockUser.id);
4751
jest.spyOn(service, 'getOne').mockReturnValue(Promise.resolve(todo));
4852
expect(await controller.getOne(todo.id)).toStrictEqual(todo);
4953
});
5054

5155
it('should be able to create a new todo', async () => {
52-
const todo = createMockTodo();
56+
const todo = createMockTodo(mockUser.id);
5357
jest.spyOn(service, 'create').mockReturnValue(Promise.resolve(todo));
5458
const res = await controller.create({ ...todo });
5559
expect(res).toStrictEqual(todo);
5660
});
5761

5862
it('should allow upserting a new todo', async () => {
59-
const todo = createMockTodo();
63+
const todo = createMockTodo(mockUser.id);
6064
jest.spyOn(service, 'upsert').mockReturnValue(Promise.resolve(todo));
6165
const res = await controller.upsertOne(todo);
6266
expect(res).toStrictEqual(todo);
6367
});
6468

6569
it('should allow updates to a single todo', async () => {
66-
const todo = createMockTodo();
70+
const todo = createMockTodo(mockUser.id);
6771
const newTitle = 'newTitle';
6872
jest
6973
.spyOn(service, 'update')

0 commit comments

Comments
 (0)