From 846992ab8f0f9f168f7441eaae6e17ccb457dcdd Mon Sep 17 00:00:00 2001 From: Alex Chew Date: Wed, 26 Jun 2019 13:30:06 -0700 Subject: [PATCH] Add unit tests for getAllUsagePlans --- .../__tests__/get-all-usage-plans-test.js | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 lambdas/shared/__tests__/get-all-usage-plans-test.js diff --git a/lambdas/shared/__tests__/get-all-usage-plans-test.js b/lambdas/shared/__tests__/get-all-usage-plans-test.js new file mode 100644 index 000000000..77ac7c16a --- /dev/null +++ b/lambdas/shared/__tests__/get-all-usage-plans-test.js @@ -0,0 +1,106 @@ +const { getAllUsagePlans } = require('../get-all-usage-plans') + +const promiser = require('../../setup-jest').promiser + +const mockUsagePlanItem = () => ({ + id: '1a2b3c', + name: '1a2b3c', + apiStages: [ + { + apiId: 'anmlcrckrs', + stage: 'prod', + }, + { + apiId: 'jlpnochips', + stage: 'beta', + }, + ], + throttle: { + burstLimit: 10, + rateLimit: 10, + }, + quota: { + limit: 10000, + offset: 0, + period: 'DAY', + } +}) + +describe('getAllUsagePlans', () => { + test('returns all usage plans, when none exist', async () => { + const mockApiGateway = { + getUsagePlans: jest.fn().mockReturnValueOnce(promiser({ + items: [] + })) + } + + const result = await getAllUsagePlans(mockApiGateway) + const mocked = mockApiGateway.getUsagePlans.mock + expect(mocked.calls.length).toBe(1) + expect(mocked.calls[0][0]).not.toHaveProperty('position') + expect(result).toHaveLength(0) + }) + + test('returns all usage plans, when only one page of usage plans exists', async () => { + const mockApiGateway = { + getUsagePlans: jest.fn().mockReturnValueOnce(promiser({ + items: [ + mockUsagePlanItem(), + mockUsagePlanItem(), + mockUsagePlanItem(), + mockUsagePlanItem(), + ] + })) + } + + const result = await getAllUsagePlans(mockApiGateway) + const mocked = mockApiGateway.getUsagePlans.mock + expect(mocked.calls.length).toBe(1) + expect(mocked.calls[0][0]).not.toHaveProperty('position') + expect(result).toHaveLength(4) + }) + + test('returns all usage plans, when multiple pages of usage plans exist', async () => { + const mockApiGateway = { + getUsagePlans: jest.fn().mockReturnValueOnce(promiser({ + items: [ + mockUsagePlanItem(), + mockUsagePlanItem(), + mockUsagePlanItem(), + mockUsagePlanItem(), + ], + position: 'qwertyuiopasdf%3D%3D', + })).mockReturnValueOnce(promiser({ + items: [ + mockUsagePlanItem(), + mockUsagePlanItem(), + mockUsagePlanItem(), + mockUsagePlanItem(), + ], + position: 'zxcvbnm1234567%3D%3D', + })).mockReturnValueOnce(promiser({ + items: [ + mockUsagePlanItem(), + mockUsagePlanItem(), + ], + })) + } + + const result = await getAllUsagePlans(mockApiGateway) + const mocked = mockApiGateway.getUsagePlans.mock + expect(mocked.calls.length).toBe(3) + expect(mocked.calls[0][0]).not.toHaveProperty('position') + expect(mocked.calls[1][0]).toHaveProperty('position', 'qwertyuiopasdf%3D%3D') + expect(mocked.calls[2][0]).toHaveProperty('position', 'zxcvbnm1234567%3D%3D') + expect(result).toHaveLength(10) + }) + + test('passes through an API Gateway request error', async () => { + const expectedError = {} + const mockApiGateway = { + getUsagePlans: jest.fn().mockReturnValueOnce(promiser(null, expectedError)) + } + + await expect(getAllUsagePlans(mockApiGateway)).rejects.toStrictEqual(expectedError) + }) +})