-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test for meta-tag-extractor.uc
- Loading branch information
1 parent
b86f891
commit 13e134b
Showing
2 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
apps/server/src/modules/meta-tag-extractor/uc/meta-tag-extractor.uc.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { setupEntities, userFactory } from '@shared/testing'; | ||
import { LegacyLogger } from '@src/core/logger'; | ||
import { AuthorizationService } from '@src/modules/authorization'; | ||
import { MetaTagExtractorService } from '../service'; | ||
import { MetaTagExtractorUc } from './meta-tag-extractor.uc'; | ||
|
||
describe(MetaTagExtractorUc.name, () => { | ||
let module: TestingModule; | ||
let uc: MetaTagExtractorUc; | ||
let authorizationService: DeepMocked<AuthorizationService>; | ||
let metaTagExtractorService: DeepMocked<MetaTagExtractorService>; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
MetaTagExtractorUc, | ||
{ | ||
provide: MetaTagExtractorService, | ||
useValue: createMock<MetaTagExtractorService>(), | ||
}, | ||
{ | ||
provide: AuthorizationService, | ||
useValue: createMock<AuthorizationService>(), | ||
}, | ||
{ | ||
provide: LegacyLogger, | ||
useValue: createMock<LegacyLogger>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
uc = module.get(MetaTagExtractorUc); | ||
authorizationService = module.get(AuthorizationService); | ||
metaTagExtractorService = module.get(MetaTagExtractorService); | ||
|
||
await setupEntities(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('fetchMetaData', () => { | ||
describe('when user exists', () => { | ||
const setup = () => { | ||
const user = userFactory.build(); | ||
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user); | ||
|
||
return { user }; | ||
}; | ||
|
||
it('should check if the user is a valid user', async () => { | ||
const { user } = setup(); | ||
|
||
authorizationService.getUserWithPermissions.mockResolvedValueOnce(user); | ||
|
||
const url = 'https://www.example.com/great-example'; | ||
await uc.fetchMetaData(user.id, url); | ||
|
||
expect(authorizationService.getUserWithPermissions).toHaveBeenCalledWith(user.id); | ||
}); | ||
|
||
it('should call meta tag extractor service', async () => { | ||
const { user } = setup(); | ||
|
||
const url = 'https://www.example.com/great-example'; | ||
await uc.fetchMetaData(user.id, url); | ||
|
||
expect(metaTagExtractorService.fetchMetaData).toHaveBeenCalledWith(url); | ||
}); | ||
}); | ||
|
||
describe('when user does not exist', () => { | ||
const setup = () => { | ||
const user = userFactory.build(); | ||
authorizationService.getUserWithPermissions.mockRejectedValue(false); | ||
|
||
return { user }; | ||
}; | ||
|
||
it('should throw an UnauthorizedException', async () => { | ||
const { user } = setup(); | ||
|
||
const url = 'https://www.example.com/great-example'; | ||
await expect(uc.fetchMetaData(user.id, url)).rejects.toThrow(UnauthorizedException); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters