Skip to content

Commit

Permalink
Added unsubscribe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-tco committed Oct 24, 2023
1 parent f7e3c30 commit a9a7a3d
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 2 deletions.
67 changes: 67 additions & 0 deletions src/notifications/v2/unsubscribe/unsubscribe.controller.spec.ts
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,
);
});
});
});
8 changes: 6 additions & 2 deletions src/notifications/v2/unsubscribe/unsubscribe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ export class UnsubscribeService {
unsubscribe.newsletterId = newsletterId;
unsubscribe.savedSearchId = savedSearchId;
unsubscribe.user = user;

return this.unsubscribeRepository.save<Unsubscribe>(unsubscribe);
// Should throw an error if the user already has an unsubscribe record
// Should throw an error if the user is not defined
if (!user) {
throw new Error('User is not defined');
}
return this.unsubscribeRepository.save(unsubscribe);
}
}

Expand Down
135 changes: 135 additions & 0 deletions src/notifications/v2/unsubscribe/unsubscrube.service.spec.ts
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();
});
});
});

0 comments on commit a9a7a3d

Please sign in to comment.