Skip to content

Commit

Permalink
test(agreement): unit test for ctrl
Browse files Browse the repository at this point in the history
  • Loading branch information
astorverse committed Aug 7, 2024
1 parent f596850 commit 78b8fce
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,30 +273,26 @@ erDiagram

## 테스트 코드 네이밍 룰

### jest 코드

```typescript
describe('{layer}', () => {
describe('{method}', async () => {
it('should {result}{condition}', () => {});
});
});
```
describe('{layer}',()=>{

describe('{method}',async ()=>{
### 예시

it('should {result}{condition}',()=>{})
})
})
```

` AgreementsService
```typescript
AgreementsService
createAgreement
should return AgreementDto with valid input (6 ms)
existCheck
should throw exception when agreement does not exist (5 ms)
should return AgreementDto when agreement exists (2 ms)
findAgreement
✓ should return AgreementDto with valid input (1 ms)
findAgreements
✓ should return AgreementDtos with valid input (1 ms)
patchAgreement
✓ should return AgreementDto with valid input (1 ms)
✓ should throw exception for invalid agreementId (1 ms)
✓ should throw exception for invalid userId (1 ms)`
```

## 접두사 정리

Expand Down
56 changes: 56 additions & 0 deletions src/APIs/agreements/__test__/agreements.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AgreementType } from '@/common/enums/agreement-type.enum';
import { AgreementsController } from '../agreements.controller';
import { AgreementCreateRequestDto } from '../dtos/request/agreement-create-request.dto';
import { Request } from 'express';
import { AgreementPatchRequestDto } from '../dtos/request/agreement-patch-request.dto';

describe('AgreementsService', () => {
let ctrl_agreements: AgreementsController;
Expand All @@ -26,6 +27,7 @@ describe('AgreementsService', () => {
},
],
}).compile();

ctrl_agreements = module.get<AgreementsController>(AgreementsController);
svc_agreements =
module.get<MockService<AgreementsService>>(AgreementsService);
Expand Down Expand Up @@ -55,4 +57,58 @@ describe('AgreementsService', () => {
});
});
});

describe('fetchAgreements', () => {
it('should fetch agreements for the user', async () => {
const req = { user: { userId: 1 } } as Request;
svc_agreements.findAgreements.mockResolvedValue([dto_agreement]);

const result = await ctrl_agreements.fetchAgreements(req);
expect(result).toEqual([dto_agreement]);
expect(svc_agreements.findAgreements).toHaveBeenCalledWith({ userId: 1 });
});
});

describe('fetchAgreementAdmin', () => {
it('should fetch agreements for the target user as admin', async () => {
const req = { user: { userId: 1 } } as Request;
const targetUserKakaoId = 2;
svc_agreements.adminCheck.mockResolvedValue(true);
svc_agreements.findAgreements.mockResolvedValue([dto_agreement]);

const result = await ctrl_agreements.fetchAgreementAdmin(
req,
targetUserKakaoId,
);
expect(result).toEqual([dto_agreement]);
expect(svc_agreements.adminCheck).toHaveBeenCalledWith({ userId: 1 });
expect(svc_agreements.findAgreements).toHaveBeenCalledWith({
userId: targetUserKakaoId,
});
});
});
describe('patchAgreement', () => {
it('should patch an agreement', async () => {
const req = { user: { userId: 1 } } as Request;
const agreementId = 1;
const patchAgreementInput: AgreementPatchRequestDto = { isAgreed: false };
const patchAgreementOutput: AgreementDto = {
...dto_agreement,
isAgreed: patchAgreementInput.isAgreed,
};
svc_agreements.patchAgreement.mockResolvedValue(patchAgreementOutput);

const result = await ctrl_agreements.patchAgreement(
req,
agreementId,
patchAgreementInput,
);
expect(result).toEqual(patchAgreementOutput);
expect(svc_agreements.patchAgreement).toHaveBeenCalledWith({
...patchAgreementInput,
agreementId,
userId: req.user.userId,
});
});
});
});
3 changes: 0 additions & 3 deletions src/APIs/agreements/agreements.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ export class AgreementsRepository extends Repository<Agreement> {
constructor(private db_dataSource: DataSource) {
super(Agreement, db_dataSource.createEntityManager());
}
getHello(): string {
return '1';
}
}

0 comments on commit 78b8fce

Please sign in to comment.