From fd5678ac69d899cb10c503adf0ddd1bede55b652 Mon Sep 17 00:00:00 2001 From: jdhasko <58209336+jdhasko@users.noreply.github.com> Date: Mon, 6 Jun 2022 13:06:32 +0200 Subject: [PATCH 1/3] 5 passing integration tests added to discount controller. --- .vscode/launch.json | 22 ++++++ .../app/discounts/discount.controller.spec.ts | 79 +++++++++++++++++++ .../src/app/discounts/discounts.constant.ts | 77 ++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 .vscode/launch.json create mode 100644 apps/api/src/app/discounts/discount.controller.spec.ts create mode 100644 apps/api/src/app/discounts/discounts.constant.ts diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..ee3b0e7 --- /dev/null +++ b/.vscode/launch.json @@ -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": [ + "/**" + ], + "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" + ] + } + ] +} \ No newline at end of file diff --git a/apps/api/src/app/discounts/discount.controller.spec.ts b/apps/api/src/app/discounts/discount.controller.spec.ts new file mode 100644 index 0000000..20598d5 --- /dev/null +++ b/apps/api/src/app/discounts/discount.controller.spec.ts @@ -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); + service = app.get(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]); + }); + + + + }); + +}); diff --git a/apps/api/src/app/discounts/discounts.constant.ts b/apps/api/src/app/discounts/discounts.constant.ts new file mode 100644 index 0000000..184945d --- /dev/null +++ b/apps/api/src/app/discounts/discounts.constant.ts @@ -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, + }, +]; From 9a45a906303580379177c6c5ed0cd373e7d2be82 Mon Sep 17 00:00:00 2001 From: jdhasko <58209336+jdhasko@users.noreply.github.com> Date: Mon, 6 Jun 2022 16:20:34 +0200 Subject: [PATCH 2/3] Checking github repo access bug --- testCommit.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 testCommit.txt diff --git a/testCommit.txt b/testCommit.txt new file mode 100644 index 0000000..19b1e62 --- /dev/null +++ b/testCommit.txt @@ -0,0 +1 @@ +test. From 69c0847f6c3c37ad07c414f3eb1ebfa2265a0bb8 Mon Sep 17 00:00:00 2001 From: jdhasko <58209336+jdhasko@users.noreply.github.com> Date: Mon, 6 Jun 2022 16:21:41 +0200 Subject: [PATCH 3/3] Removing test document - github access test --- testCommit.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 testCommit.txt diff --git a/testCommit.txt b/testCommit.txt deleted file mode 100644 index 19b1e62..0000000 --- a/testCommit.txt +++ /dev/null @@ -1 +0,0 @@ -test.