Skip to content

Commit 8db20e2

Browse files
started test for offer handler (lambda)
1 parent 5557a38 commit 8db20e2

File tree

3 files changed

+196
-2
lines changed

3 files changed

+196
-2
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
let offerService = require('../../api/services/offerService');
2+
let locationService = require('../../api/services/locationService');
3+
let brandService = require('../../api/services/brandService');
4+
5+
const offerHandler = require('../../api/offer');
6+
7+
// jest.mock('../../api/services/offerService');
8+
// jest.mock('../../api/services/locationService');
9+
// jest.mock('../../api/services/brandService');
10+
11+
const offerParams = {
12+
name: "Super Duper Offer",
13+
brandId: "692126c8-6e72-4ad7-8a73-25fc2f1f56e4",
14+
startDate: "1633616707",
15+
};
16+
17+
let mockedOffer = {
18+
...offerParams,
19+
id: "d9b1d9ff-543e-47c7-895f-87f71dcad91b",
20+
publisherId: "d9b1d9ff-543e-47c7-895f-87f71dcad91b",
21+
};
22+
23+
let mockedLocation = {
24+
id: "d9b1d9ff-543e-47c7-895f-87f71dcad91b",
25+
address: "Charlie Brown Street, 68",
26+
brandId: "692126c8-6e72-4ad7-8a73-25fc2f1f56e4",
27+
hasOffer: false,
28+
};
29+
30+
let mockedBrand = {
31+
id: "692126c8-6e72-4ad7-8a73-25fc2f1f56e4",
32+
name: "Starbucks",
33+
};
34+
35+
const showEvent = { pathParameters: { offerId: mockedOffer.id } };
36+
const createEvent = { body: JSON.stringify(offerParams) };
37+
38+
const linkToLocationEvent = {
39+
pathParameters: {
40+
offerId: mockedOffer.id,
41+
locationId: mockedLocation.id,
42+
}
43+
};
44+
45+
const linkAllBrandsLocationToAnOfferEvent = {
46+
pathParameters: {
47+
offerId: mockedOffer.id,
48+
brandId: mockedBrand.id,
49+
}
50+
};
51+
52+
describe('Test Offer main lambda function >', () => {
53+
// beforeAll(() => {
54+
55+
// });
56+
57+
afterAll(() => {
58+
jest.clearAllMocks();
59+
});
60+
61+
describe('index', () => {
62+
beforeEach(() => {
63+
jest.clearAllMocks();
64+
})
65+
66+
it('should return all offers, with status 200', async () => {
67+
const arrayOfMmockedOffers = [ mockedOffer, mockedOffer ];
68+
69+
offerService.getAll = jest.fn();
70+
offerService.getAll.mockReturnValue(arrayOfMmockedOffers);
71+
72+
const response = await offerHandler.index();
73+
74+
expect(offerService.getAll).toHaveBeenCalledTimes(1);
75+
76+
expect(response.statusCode).toBe(200);
77+
expect(response.body).toBe(JSON.stringify({
78+
message: 'Offers successfully fetched',
79+
offers: arrayOfMmockedOffers,
80+
}))
81+
82+
expect.assertions(3);
83+
});
84+
85+
describe('when has no offer', () => {
86+
it('should return status 404', async () => {
87+
offerService.getAll = jest.fn();
88+
offerService.getAll.mockReturnValue(null);
89+
90+
const response = await offerHandler.index();
91+
92+
expect(response.statusCode).toBe(404);
93+
expect(response.body).toBe(JSON.stringify({
94+
message: 'No offers was found!',
95+
}))
96+
97+
expect.assertions(2);
98+
});
99+
})
100+
});
101+
102+
describe('getById', () => {
103+
beforeEach(() => {
104+
jest.clearAllMocks();
105+
})
106+
107+
it('should return the given offer, with status 200', async () => {
108+
offerService.getAll = jest.fn();
109+
offerService.getAll.mockReturnValue(mockedOffer);
110+
111+
const response = await offerHandler.show(showEvent);
112+
113+
expect(offerService.getById).toHaveBeenCalledTimes(1);
114+
expect(offerService.getById).toHaveBeenCalledWith(mockedOffer.id);
115+
116+
expect(response.statusCode).toBe(200);
117+
expect(response.body).toBe(JSON.stringify({
118+
message: 'Offer successfully found',
119+
offer: mockedOffer,
120+
}))
121+
122+
expect.assertions(3);
123+
});
124+
125+
describe('when has no offer', () => {
126+
it('should return status 404', async () => {
127+
offerService.getAll = jest.fn();
128+
offerService.getAll.mockReturnValue(null);
129+
130+
const response = await offerHandler.index();
131+
132+
expect(response.statusCode).toBe(404);
133+
expect(response.body).toBe(JSON.stringify({
134+
message: 'No offers was found!',
135+
}))
136+
137+
expect.assertions(2);
138+
});
139+
})
140+
});
141+
142+
describe('create', () => {
143+
beforeEach(() => {
144+
jest.clearAllMocks();
145+
})
146+
147+
it('should create and return offer, with status 200', async () => {
148+
offerService.create = jest.fn();
149+
offerService.create.mockReturnValue(mockedOffer);
150+
151+
const response = await offerHandler.create(createEvent);
152+
153+
expect(offerService.create).toHaveBeenCalledTimes(1);
154+
expect(offerService.create).toHaveBeenCalledWith(offerParams);
155+
156+
expect(response.statusCode).toBe(200);
157+
expect(response.body).toBe(JSON.stringify({
158+
message: 'Offer successfully created!',
159+
offer: mockedOffer
160+
}))
161+
162+
expect.assertions(4);
163+
});
164+
165+
describe('when has required value missing', () => {
166+
it('should return status 400', async () => {
167+
const response = await offerHandler.create({ body: JSON.stringify({}) });
168+
169+
expect(response.statusCode).toBe(400);
170+
expect(response.body).toBe(JSON.stringify({
171+
message: 'Could not create offer, invalid or missing params',
172+
}))
173+
174+
expect.assertions(2);
175+
});
176+
})
177+
178+
describe('when has an unexpected problem on creation', () => {
179+
it('should return status 500', async () => {
180+
offerService.create = jest.fn();
181+
offerService.create.mockReturnValue(false);
182+
183+
const response = await offerHandler.create(createEvent);
184+
185+
expect(response.statusCode).toBe(500);
186+
expect(response.body).toBe(JSON.stringify({
187+
message: 'Could not create this offer',
188+
}))
189+
190+
expect.assertions(2);
191+
});
192+
})
193+
});
194+
});

‎__tests__/services/offerService.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const locationTableName = process.env.LOCATION_TABLE;
1818
const offerParams = {
1919
name: "Super Duper Offer",
2020
brandId: "692126c8-6e72-4ad7-8a73-25fc2f1f56e4",
21-
locationsTotal: 0,
2221
};
2322

2423
let mockedOffer = {
2524
...offerParams,
2625
id: "d9b1d9ff-543e-47c7-895f-87f71dcad91b",
2726
publisherId: "d9b1d9ff-543e-47c7-895f-87f71dcad91b",
27+
locationsTotal: 0,
2828
};
2929

3030
let mockedLocation = {

‎api/offer.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const locationService = require('./services/locationService');
77
module.exports.index = async (event) => {
88
console.log(`${__MODULE__}@index: Fetch all offers`, event);
99

10-
const offers = await locationService.getAll();
10+
const offers = await offerService.getAll();
1111

1212
if (!offers) {
1313
const body = { message: 'No offers was found!' };

0 commit comments

Comments
 (0)