Skip to content
This repository was archived by the owner on Dec 9, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Debug api",
"skipFiles": [
"<node_internals>/**"
],
"runtimeArgs": ["-r","ts-node/register","-r","tsconfig-paths/register"],
"console": "integratedTerminal",
"program": "${workspaceFolder}\\apps\\api\\src\\app\\discounts\\discount.controller.spec.ts",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
79 changes: 79 additions & 0 deletions apps/api/src/app/discounts/discount.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { Test, TestingModule } from '@nestjs/testing';
import { Console } from 'console';
import { of } from 'rxjs';
import { discounts } from './discounts.constant';
import { DiscountsController as Controller } from './discounts.controller';
import { DiscountsService as Service } from './discounts.service';

describe('Discount Controller', () => {
// Reference variables used for spying
let app: TestingModule;
let controller: Controller;
let service: Service;

const discountId = '78c0a432-59f8-4a6c-b89d-57030a6628d9'; // Corresponds to discount[0]

beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [Controller],
providers: [
// Mock of the service and its methods
{
provide: Service,
useFactory: () => ({
findAll: jest.fn().mockReturnValue(discounts), // an observale of discount list
findOneByCode: jest.fn().mockReturnValue(discounts[0]), // a promise turned into a singular discount item
}),
},
],
}).compile();
// Set the references
controller = app.get<Controller>(Controller);
service = app.get<Service>(Service);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('getAll()', () => {
it('should call findAll 1 time"', async () => {
jest.spyOn(service, 'findAll');

await controller.getAll();
expect(service.findAll).toHaveBeenCalledTimes(1);
expect(service.findAll).toHaveBeenCalledWith();
});

it('should call findAll and receive a list of discounts"', async () => {
jest.spyOn(service, 'findAll');

const response = await controller.getAll();
console.log(response)
expect(response).toEqual(discounts);
});

});

describe('get()', ()=>{
it('should call findOne 1 time',async () => {
jest.spyOn(service, 'findOneByCode');

await controller.get(discountId);
expect(service.findOneByCode).toHaveBeenCalledTimes(1);
expect(service.findOneByCode).toHaveBeenCalledWith(discountId);
});

it('should call findOne and receive a single discount"', async () => {
jest.spyOn(service, 'findOneByCode');

const response = await controller.get(discountId);
expect(response).toEqual(discounts[0]);
});



});

});
77 changes: 77 additions & 0 deletions apps/api/src/app/discounts/discounts.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DiscountTypeEnum, IDiscount } from '@interfaces';

export const discounts: IDiscount[] = [
{
discountId: '78c0a432-59f8-4a6c-b89d-57030a6628d9',
code: 'discount-100',
amount: 100,
type: DiscountTypeEnum.amount,
remainingUses: 100,
startsAt: new Date('1977-12-26 12:30:00'),
isEnabled: true,
},
{
discountId: 'b43eba0f-3a6e-4864-8206-89880e78b4d3',
code: 'discount-666666',
amount: 666666,
type: DiscountTypeEnum.amount,
remainingUses: 100,
startsAt: new Date('1977-12-26 12:30:00'),
isEnabled: true,
},
{
discountId: '2df8ea99-bcf0-47ec-a6b3-388e8a9e3e74',
code: 'discount-10-percent',
amount: 10,
type: DiscountTypeEnum.percentage,
remainingUses: 100,
startsAt: new Date('2020-3-13 12:30:00'),
isEnabled: true,
},
{
discountId: 'a7f0b654-dea1-404c-a725-3e0046c459a4',
code: 'discount-100-percent',
amount: 100,
type: DiscountTypeEnum.percentage,
remainingUses: 100,
startsAt: new Date('2020-3-13 12:30:00'),
isEnabled: true,
},
{
discountId: '1d7d4480-1753-4846-b05d-37fe678256ed',
code: 'discount-expired',
amount: 100,
type: DiscountTypeEnum.amount,
remainingUses: 100,
startsAt: new Date('2012-12-21 08:30:00'),
expiresAt: new Date('2012-12-21 23:59:59'),
isEnabled: true,
},
{
discountId: '73b993b4-e793-47cb-89d0-cd1aefed53f6',
code: 'discount-not-started',
amount: 100,
type: DiscountTypeEnum.amount,
remainingUses: 100,
startsAt: new Date('2112-12-21 21:12:21'),
isEnabled: true,
},
{
discountId: '8187f8a3-06c2-4982-ae82-fc547efd27f4',
code: 'discount-disabled',
amount: 100,
type: DiscountTypeEnum.amount,
remainingUses: 100,
startsAt: new Date('1974-02-08 00:00:01'),
isEnabled: false,
},
{
discountId: 'a82a71e7-5012-4b57-8780-c7b598ff99d6',
code: 'discount-no-uses',
amount: 100,
type: DiscountTypeEnum.amount,
remainingUses: 0,
startsAt: new Date('2022-01-01 10:10:10'),
isEnabled: false,
},
];