-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/notifications/v2/unsubscribe/unsubscribe.controller.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,67 @@ | ||
import { async } from 'rxjs'; | ||
import { UnsubscribeController } from './unsubscribe.controller'; | ||
import { UnsubscribeService } from './unsubscribe.service'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
describe('UnsubscribeController', () => { | ||
let unsubscribeController: UnsubscribeController; | ||
let unsubscribeService: UnsubscribeService; | ||
|
||
const mockFindOne = jest.fn(); | ||
const mockDelete = jest.fn(); | ||
|
||
beforeEach(jest.clearAllMocks); | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [UnsubscribeController], | ||
providers: [ | ||
{ | ||
provide: UnsubscribeService, | ||
useValue: { | ||
findOneById: mockFindOne, | ||
deleteOneById: mockDelete, | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
unsubscribeService = module.get<UnsubscribeService>(UnsubscribeService); | ||
unsubscribeController = module.get<UnsubscribeController>( | ||
UnsubscribeController, | ||
); | ||
}); | ||
|
||
const mockUnsubscribe = { | ||
id: 1, | ||
user: { | ||
id: 1, | ||
email: '[email protected]', | ||
}, | ||
subscriptionId: '123', | ||
newsletterId: 'NEW_GRANTS', | ||
savedSearchId: 1, | ||
}; | ||
|
||
it('should be defined', () => { | ||
expect(unsubscribeController).toBeDefined(); | ||
expect(unsubscribeService).toBeDefined(); | ||
}); | ||
|
||
describe('findOne', () => { | ||
it('should return an unsubscribe object', async () => { | ||
mockFindOne.mockResolvedValueOnce(mockUnsubscribe); | ||
expect(await unsubscribeController.findOne('1')).toBe( | ||
mockUnsubscribe, | ||
); | ||
}); | ||
}); | ||
|
||
describe('delete', () => { | ||
it('should return an unsubscribe object', async () => { | ||
mockDelete.mockResolvedValueOnce(mockUnsubscribe); | ||
expect(await unsubscribeController.delete('1')).toBe( | ||
mockUnsubscribe, | ||
); | ||
}); | ||
}); | ||
}); |
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
135 changes: 135 additions & 0 deletions
135
src/notifications/v2/unsubscribe/unsubscrube.service.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,135 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { UnsubscribeService } from './unsubscribe.service'; | ||
import { Repository } from 'typeorm'; | ||
import { NewsletterType, Unsubscribe } from './unsubscribe.entity'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
|
||
describe('UnsubscribeService', () => { | ||
let unsubscribeService: UnsubscribeService; | ||
let unsubscribeRepository: Repository<Unsubscribe>; | ||
|
||
beforeEach(jest.clearAllMocks); | ||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [ | ||
UnsubscribeService, | ||
{ | ||
provide: getRepositoryToken(Unsubscribe), | ||
useValue: { | ||
findOne: jest.fn(), | ||
delete: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
unsubscribeService = module.get(UnsubscribeService); | ||
unsubscribeRepository = module.get<Repository<Unsubscribe>>( | ||
getRepositoryToken(Unsubscribe), | ||
); | ||
}); | ||
const mockUnsubscribe = { | ||
id: 1, | ||
user: { | ||
id: 1, | ||
email: '[email protected]', | ||
}, | ||
subscriptionId: '123', | ||
newsletterId: 'NEW_GRANTS', | ||
savedSearchId: 1, | ||
}; | ||
|
||
it('should be defined', () => { | ||
expect(unsubscribeService).toBeDefined(); | ||
expect(unsubscribeRepository).toBeDefined(); | ||
}); | ||
|
||
describe('findOneById', () => { | ||
it('should return a unsubscribe', async () => { | ||
unsubscribeRepository.findOne = jest | ||
.fn() | ||
.mockResolvedValueOnce(mockUnsubscribe); | ||
const result = await unsubscribeService.findOneById('1'); | ||
expect(result).toEqual(mockUnsubscribe); | ||
}); | ||
}); | ||
|
||
describe('deleteOneById', () => { | ||
it('should delete a unsubscribe', async () => { | ||
unsubscribeRepository.delete = jest | ||
.fn() | ||
.mockResolvedValueOnce(mockUnsubscribe); | ||
const result = await unsubscribeService.deleteOneById('1'); | ||
expect(result).toEqual(mockUnsubscribe); | ||
}); | ||
}); | ||
|
||
describe('findOneBySubscriptionIdTypeAndUser', () => { | ||
it('should return a unsubscribe', async () => { | ||
unsubscribeRepository.findOne = jest | ||
.fn() | ||
.mockResolvedValueOnce(mockUnsubscribe); | ||
const result = | ||
await unsubscribeService.findOneBySubscriptionIdTypeAndUser( | ||
'123', | ||
NewsletterType.NEW_GRANTS, | ||
1, | ||
{ | ||
id: 1, | ||
hashedEmailAddress: 'ahash', | ||
encryptedEmailAddress: 'aenc', | ||
sub: 'asub', | ||
subscriptions: [], | ||
unsubscribeReferences: [], | ||
savedSearches: [], | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
newsletterSubscriptions: [], | ||
savedSearchNotifications: [], | ||
}, | ||
); | ||
expect(result).toEqual(mockUnsubscribe); | ||
}); | ||
}); | ||
|
||
describe('create', () => { | ||
it('should create a unsubscribe', async () => { | ||
unsubscribeRepository.save = jest | ||
.fn() | ||
.mockResolvedValueOnce(mockUnsubscribe); | ||
const result = await unsubscribeService.create({ | ||
user: { | ||
id: 1, | ||
hashedEmailAddress: 'ahash', | ||
encryptedEmailAddress: 'aenc', | ||
sub: 'asub', | ||
subscriptions: [], | ||
unsubscribeReferences: [], | ||
savedSearches: [], | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
newsletterSubscriptions: [], | ||
savedSearchNotifications: [], | ||
}, | ||
subscriptionId: '123', | ||
newsletterId: NewsletterType.NEW_GRANTS, | ||
savedSearchId: 1, | ||
}); | ||
expect(result).toEqual(mockUnsubscribe); | ||
}); | ||
|
||
it('should throw an error if the user is not defined', async () => { | ||
unsubscribeRepository.findOne = jest | ||
.fn() | ||
.mockResolvedValueOnce(mockUnsubscribe); | ||
await expect( | ||
unsubscribeService.create({ | ||
user: undefined, | ||
subscriptionId: '123', | ||
newsletterId: NewsletterType.NEW_GRANTS, | ||
savedSearchId: 1, | ||
}), | ||
).rejects.toThrowError(); | ||
}); | ||
}); | ||
}); |