Skip to content

Commit 5f520f3

Browse files
committed
Update unit tests
1 parent 9fccd02 commit 5f520f3

5 files changed

Lines changed: 129 additions & 3 deletions

File tree

jest.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// https://jestjs.io/docs/configuration
22

33
module.exports = {
4-
testMatch: ["**/?(*.)+(spec).js?(x)", "**/?(*.)+(spec).ts?(x)"],
4+
testMatch: ["**/?(*.)+(spec).js?(x)", "**/?(*.)+(spec).ts?(x)"],
5+
collectCoverageFrom: [
6+
'product_service/lambdas/*.js',
7+
'!product_service/lambdas/constants.js',
8+
'!product_service/lambdas/cors.js',
9+
],
510
};

product_service/lambdas/getProductsById.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ exports.handler = async (event) => {
4040
dynamoDB.send(new QueryCommand(stockParams))
4141
]);
4242

43-
const product = productResponse.Items[0];
44-
const stock = stockResponse.Items[0];
43+
const product = productResponse?.Items[0];
44+
const stock = stockResponse?.Items[0];
4545

4646
if (product) {
4747
const returnedBody = JSON.stringify({
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { validateInput } = require('../lambdas/validation');
2+
const { MESSAGES } = require('../lambdas/constants');
3+
const ProductBuilder = require('../utils/productBuilder');
4+
5+
describe('validateInput function', () => {
6+
it('should return null if input is valid', () => {
7+
const input = new ProductBuilder().build();
8+
const result = validateInput(input);
9+
expect(result).toBeNull();
10+
});
11+
12+
describe.each([{}, null, undefined, NaN, '', '\t', '\n'])(
13+
'should return INVALID_INPUT_TITLE if description is "%s"',
14+
(title) => {
15+
it(`should return ${MESSAGES.INVALID_INPUT_TITLE}`, () => {
16+
const input = new ProductBuilder().withTitle(title).build();
17+
const result = validateInput(input);
18+
expect(result).toEqual(MESSAGES.INVALID_INPUT_TITLE);
19+
});
20+
}
21+
);
22+
23+
describe.each([{}, null, undefined, NaN, '', '\t', '\n'])(
24+
'should return INVALID_INPUT_DESCRIPTION if description is "%s"',
25+
(description) => {
26+
it(`should return ${MESSAGES.INVALID_INPUT_DESCRIPTION}`, () => {
27+
const input = new ProductBuilder().withDescription(description).build();
28+
const result = validateInput(input);
29+
expect(result).toEqual(MESSAGES.INVALID_INPUT_DESCRIPTION);
30+
});
31+
}
32+
);
33+
34+
describe.each([-2, -1, {}, null, undefined, NaN, '', '\t', '\n'])(
35+
'should return INVALID_INPUT_PRICE if price is invalid: "%s"',
36+
(price) => {
37+
it('should return INVALID_INPUT_PRICE', () => {
38+
const input = new ProductBuilder().withPrice(price).build();
39+
const result = validateInput(input);
40+
expect(result).toEqual(MESSAGES.INVALID_INPUT_PRICE);
41+
});
42+
}
43+
);
44+
45+
describe.each([-2, -1, {}, null, undefined, NaN, '', '\t', '\n'])(
46+
'should return INVALID_INPUT_COUNT if count is less than zero: "%s"',
47+
(count) => {
48+
it('should return INVALID_INPUT_COUNT', () => {
49+
const input = new ProductBuilder().withCount(count).build();
50+
const result = validateInput(input);
51+
expect(result).toEqual(MESSAGES.INVALID_INPUT_COUNT);
52+
});
53+
}
54+
);
55+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class ProductBuilder {
2+
constructor() {
3+
this.product = {
4+
id: 'ead768ea-8752-4417-a94d-e8eaf256c500',
5+
title: 'some title',
6+
description: 'some description',
7+
price: 100,
8+
count: 50
9+
};
10+
}
11+
12+
withId(id) {
13+
this.product.id = id;
14+
return this;
15+
}
16+
17+
withTitle(title) {
18+
this.product.title = title;
19+
return this;
20+
}
21+
22+
withDescription(description) {
23+
this.product.description = description;
24+
return this;
25+
}
26+
27+
withPrice(price) {
28+
this.product.price = price;
29+
return this;
30+
}
31+
32+
withCount(count) {
33+
this.product.count = count;
34+
return this;
35+
}
36+
37+
build() {
38+
return this.product;
39+
}
40+
}
41+
42+
module.exports = ProductBuilder;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class StockBuilder {
2+
constructor() {
3+
this.stock = {
4+
product_id: 'ead768ea-8752-4417-a94d-e8eaf256c500',
5+
count: 50,
6+
};
7+
}
8+
9+
withId(product_id) {
10+
this.stock.product_id = product_id;
11+
return this;
12+
}
13+
14+
withCount(count) {
15+
this.stock.count = count;
16+
return this;
17+
}
18+
19+
build() {
20+
return this.stock;
21+
}
22+
}
23+
24+
module.exports = StockBuilder;

0 commit comments

Comments
 (0)