|
| 1 | +const AWS = require('aws-sdk'); |
| 2 | +AWS.config.update({ region:'us-east-1' }); |
| 3 | + |
| 4 | +const dynamoDb = require('aws-sdk/clients/dynamodb'); |
| 5 | +require('dotenv').config() |
| 6 | + |
| 7 | +const locationService = require('../../api/services/locationService'); |
| 8 | + |
| 9 | +let dynamoDbPut; |
| 10 | +let dynamoDbGet; |
| 11 | +let dynamoDbScan; |
| 12 | + |
| 13 | +const locationTableName = process.env.LOCATION_TABLE; |
| 14 | + |
| 15 | +const locationParams = { |
| 16 | + address: "Konohagakure, 172", |
| 17 | + brandId: "692126c8-6e72-4ad7-8a73-25fc2f1f56e4", |
| 18 | + hasOffer: false, |
| 19 | +}; |
| 20 | + |
| 21 | +let mockedLocation = { |
| 22 | + ...locationParams, |
| 23 | + id: "d9b1d9ff-543e-47c7-895f-87f71dcad91b", |
| 24 | +}; |
| 25 | + |
| 26 | +describe('Test LocationService >', () => { |
| 27 | + beforeAll(() => { |
| 28 | + dynamoDbPut = jest.spyOn(dynamoDb.DocumentClient.prototype, 'put'); |
| 29 | + dynamoDbGet = jest.spyOn(dynamoDb.DocumentClient.prototype, 'get'); |
| 30 | + dynamoDbScan = jest.spyOn(dynamoDb.DocumentClient.prototype, 'scan'); |
| 31 | + }); |
| 32 | + |
| 33 | + afterAll(() => { |
| 34 | + jest.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('create', () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + }) |
| 41 | + |
| 42 | + it('should create item on db and return location', async () => { |
| 43 | + dynamoDbPut.mockReturnValue({ |
| 44 | + promise: () => Promise.resolve('data'), |
| 45 | + }); |
| 46 | + |
| 47 | + const location = await locationService.create(locationParams); |
| 48 | + |
| 49 | + expect(location).toHaveProperty('id'); |
| 50 | + expect(location).toHaveProperty('address', locationParams.address); |
| 51 | + expect(location).toHaveProperty('brandId', locationParams.brandId); |
| 52 | + expect(location).toHaveProperty('hasOffer', false); |
| 53 | + expect(location).toHaveProperty('created'); |
| 54 | + expect(location).toHaveProperty('updated'); |
| 55 | + |
| 56 | + expect(dynamoDbPut).toHaveBeenCalledTimes(1); |
| 57 | + expect(dynamoDbPut).toHaveBeenCalledWith({ |
| 58 | + TableName: locationTableName, |
| 59 | + Item: expect.any(Object), |
| 60 | + }); |
| 61 | + |
| 62 | + expect.assertions(8); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should log error and return false on failure', async () => { |
| 66 | + jest.spyOn(console, 'error'); |
| 67 | + |
| 68 | + const rejectionObject = { error: 'foo-bar'}; |
| 69 | + |
| 70 | + dynamoDbPut.mockReturnValue({ |
| 71 | + promise: () => Promise.reject(rejectionObject), |
| 72 | + }); |
| 73 | + |
| 74 | + await locationService.create(locationParams); |
| 75 | + |
| 76 | + expect(dynamoDbPut).toHaveBeenCalledTimes(1); |
| 77 | + |
| 78 | + expect(console.error).toHaveBeenCalledTimes(1); |
| 79 | + expect(console.error).toHaveBeenCalledWith( |
| 80 | + 'LocationService@create: An error ocurred for location creation', |
| 81 | + rejectionObject |
| 82 | + ); |
| 83 | + |
| 84 | + expect.assertions(3); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('getById', () => { |
| 89 | + beforeEach(() => { |
| 90 | + jest.clearAllMocks(); |
| 91 | + }) |
| 92 | + |
| 93 | + it('should get item on db', async () => { |
| 94 | + dynamoDbGet.mockReturnValue({ |
| 95 | + promise: () => Promise.resolve({ Item: mockedLocation }), |
| 96 | + }); |
| 97 | + |
| 98 | + const location = await locationService.getById(mockedLocation.id); |
| 99 | + |
| 100 | + expect(location).toBeDefined(); |
| 101 | + expect(typeof location).toBe('object'); |
| 102 | + |
| 103 | + expect(dynamoDbGet).toHaveBeenCalledTimes(1); |
| 104 | + expect(dynamoDbGet).toHaveBeenCalledWith({ |
| 105 | + TableName: locationTableName, |
| 106 | + Key: { id: mockedLocation.id }, |
| 107 | + }); |
| 108 | + |
| 109 | + expect.assertions(4); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should return null when item does not exists on db', async () => { |
| 113 | + dynamoDbGet.mockReturnValue({ |
| 114 | + promise: () => Promise.resolve(null), |
| 115 | + }); |
| 116 | + |
| 117 | + const location = await locationService.getById(123); |
| 118 | + |
| 119 | + expect(location).toBeNull(); |
| 120 | + |
| 121 | + expect(dynamoDbGet).toHaveBeenCalledTimes(1); |
| 122 | + expect(dynamoDbGet).toHaveBeenCalledWith({ |
| 123 | + TableName: locationTableName, |
| 124 | + Key: { id: 123 }, |
| 125 | + }); |
| 126 | + |
| 127 | + expect.assertions(3); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('getAll', () => { |
| 132 | + beforeEach(() => { |
| 133 | + jest.clearAllMocks(); |
| 134 | + }) |
| 135 | + |
| 136 | + it('should get all items on db', async () => { |
| 137 | + dynamoDbScan.mockReturnValue({ |
| 138 | + promise: () => Promise.resolve({ Items: [ mockedLocation, mockedLocation ] }), |
| 139 | + }); |
| 140 | + |
| 141 | + const locations = await locationService.getAll(); |
| 142 | + |
| 143 | + expect(locations).toBeDefined(); |
| 144 | + expect(Array.isArray(locations)).toBeTruthy(); |
| 145 | + expect(locations.length).toBe(2); |
| 146 | + |
| 147 | + expect(dynamoDbScan).toHaveBeenCalledTimes(1); |
| 148 | + expect(dynamoDbScan).toHaveBeenCalledWith({ TableName: locationTableName }); |
| 149 | + |
| 150 | + expect.assertions(5); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should return null when item does not exists on db', async () => { |
| 154 | + dynamoDbScan.mockReturnValue({ |
| 155 | + promise: () => Promise.resolve(null), |
| 156 | + }); |
| 157 | + |
| 158 | + const location = await locationService.getAll(); |
| 159 | + |
| 160 | + expect(location).toBeNull(); |
| 161 | + |
| 162 | + expect(dynamoDbScan).toHaveBeenCalledTimes(1); |
| 163 | + expect(dynamoDbScan).toHaveBeenCalledWith({ TableName: locationTableName }); |
| 164 | + |
| 165 | + expect.assertions(3); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments