-
Notifications
You must be signed in to change notification settings - Fork 0
/
saving-urls.test.js
114 lines (81 loc) · 2.87 KB
/
saving-urls.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
const AWS = require('aws-sdk-mock');
const saveUrls = require('./save-urls');
const randString = () => Math.random().toString(36).substr(2, 5);
const randNumber = () => Math.ceil(5 * Math.random()) + 1;
const tableName = randString();
beforeEach(() => {
process.env.TABLE_NAME = tableName;
AWS.restore();
});
describe('generating id', () => {
it('generates a new id', async () => {
const retries = randNumber();
const response = await saveUrls.generateId({retries});
expect(response.shortId).toMatch(new RegExp(`^[-A-Za-z0-9_]{${retries + 2}}$`));
});
it('passes through the input url', async () => {
const url = randString();
const response = await saveUrls.generateId({url});
expect(response.url).toMatch(url);
});
});
describe('retry guard', () => {
it('initialises the retry guard', async () => {
const result = await saveUrls.retryGuard({});
expect(result.retries).toBe(0);
});
it('increments the retry guard', async () => {
const result = await saveUrls.retryGuard({retries: 0});
expect(result.retries).toBe(1);
});
it('passes through input properties', async () => {
const otherProp = randString();
const result = await saveUrls.retryGuard({otherProp});
expect(result.otherProp).toMatch(otherProp);
});
});
describe('storing urls', () => {
it('saves the url', async () => {
const url = randString();
const shortId = randString();
const dynamoMock = AWS.mock('DynamoDB.DocumentClient', 'put', (params, callback) => {
expect(params.TableName).toMatch(tableName);
expect(params.Item.shortId).toMatch(shortId);
expect(params.Item.url).toMatch(url);
callback();
});
await saveUrls.saveUrl({
shortId,
url
});
expect(dynamoMock.stub.calledOnce).toBeTruthy();
});
it('does not overwrite an existing url', async () => {
const url = randString();
const shortId = randString();
const dynamoMock = AWS.mock('DynamoDB.DocumentClient', 'put', (params, callback) => {
expect(params.Expected.shortId.Exists).toBeFalsy();
callback();
});
await saveUrls.saveUrl({
shortId,
url
});
expect(dynamoMock.stub.calledOnce).toBeTruthy();
});
it('returns the input', async () => {
const url = randString();
const shortId = randString();
const otherProp = randString();
AWS.mock('DynamoDB.DocumentClient', 'put');
const result = await saveUrls.saveUrl({
shortId,
url,
otherProp
});
expect(result.shortId).toMatch(shortId);
expect(result.url).toMatch(url);
expect(result.otherProp).toMatch(otherProp);
})
});