## 概要 [Task2835: 修正①(accounts,auth,Repositoiesのaccounts,common)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2835) - features - accounts - auth - common - repositories - accounts - 各entity - Nullableの項目の`@Column`デコレータに`type`を追加しないとTypeORMがエラーになりテストが通らないので追加 - https://qiita.com/maruware/items/08c9ad594e14e4ea1497#%E5%95%8F%E9%A1%8C ## レビューポイント - コメントとして記載 ## UIの変更 - Before/Afterのスクショなど - スクショ置き場 ## 動作確認状況 - ローカルで確認、develop環境で確認など ## 補足 - レビュー完了後、TODOコメント(strictNullChecks対応)は削除します
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
import { ConfigModule } from '@nestjs/config';
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { NotificationhubService } from '../../../gateways/notificationhub/notificationhub.service';
|
|
import { User } from '../../../repositories/users/entity/user.entity';
|
|
import { NotificationService } from '../notification.service';
|
|
import { UsersRepositoryService } from '../../../repositories/users/users.repository.service';
|
|
|
|
export type UsersRepositoryMockValue = {
|
|
findUserByExternalId: User | Error;
|
|
};
|
|
|
|
export type NotificationHubMockValue = {
|
|
register: undefined | Error;
|
|
};
|
|
|
|
export const makeNotificationServiceMock = async (
|
|
usersRepositoryMockValue: UsersRepositoryMockValue,
|
|
notificationHubMockValue: NotificationHubMockValue,
|
|
): Promise<NotificationService> => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [NotificationService],
|
|
imports: [
|
|
ConfigModule.forRoot({
|
|
envFilePath: ['.env.local', '.env'],
|
|
}),
|
|
],
|
|
})
|
|
.useMocker((token) => {
|
|
switch (token) {
|
|
case UsersRepositoryService:
|
|
return makeUsersRepositoryMock(usersRepositoryMockValue);
|
|
case NotificationhubService:
|
|
return makeNotificationHubMock(notificationHubMockValue);
|
|
}
|
|
})
|
|
.compile();
|
|
|
|
return module.get<NotificationService>(NotificationService);
|
|
};
|
|
|
|
export const makeNotificationHubMock = (value: NotificationHubMockValue) => {
|
|
const { register } = value;
|
|
|
|
return {
|
|
register:
|
|
register instanceof Error
|
|
? jest
|
|
.fn<Promise<void>, [number, string, string, string]>()
|
|
.mockRejectedValue(register)
|
|
: jest
|
|
.fn<Promise<void>, [number, string, string, string]>()
|
|
.mockResolvedValue(register),
|
|
};
|
|
};
|
|
|
|
export const makeUsersRepositoryMock = (value: UsersRepositoryMockValue) => {
|
|
const { findUserByExternalId } = value;
|
|
|
|
return {
|
|
findUserByExternalId:
|
|
findUserByExternalId instanceof Error
|
|
? jest.fn<Promise<void>, []>().mockRejectedValue(findUserByExternalId)
|
|
: jest.fn<Promise<User>, []>().mockResolvedValue(findUserByExternalId),
|
|
};
|
|
};
|
|
|
|
export const makeDefaultNotificationHubMockValue =
|
|
(): NotificationHubMockValue => {
|
|
return { register: undefined };
|
|
};
|
|
|
|
// 個別のテストケースに対応してそれぞれのMockを用意するのは無駄が多いのでテストケース内で個別の値を設定する
|
|
export const makeDefaultUsersRepositoryMockValue =
|
|
(): UsersRepositoryMockValue => {
|
|
const user = new User();
|
|
user.id = 2;
|
|
user.external_id = 'external_id';
|
|
user.account_id = 123;
|
|
user.role = 'none';
|
|
user.author_id = null;
|
|
user.accepted_eula_version = '1.0';
|
|
user.accepted_dpa_version = '1.0';
|
|
user.email_verified = true;
|
|
user.auto_renew = false;
|
|
user.license_alert = false;
|
|
user.notification = false;
|
|
user.deleted_at = null;
|
|
user.created_by = 'test';
|
|
user.created_at = new Date();
|
|
user.updated_by = 'test';
|
|
user.updated_at = new Date();
|
|
|
|
return {
|
|
findUserByExternalId: user,
|
|
};
|
|
};
|