-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
77 lines (67 loc) · 1.97 KB
/
index.test.js
File metadata and controls
77 lines (67 loc) · 1.97 KB
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
const AWS = require("aws-sdk");
const { AWSLambdaDataSource } = require("./index");
const awsOptions = { region: "us-west-2" };
const invokeOptions = { FunctionName: "dummy-function" };
const mockData = {
FunctionName: "dummy-function",
InvocationType: "RequestResponse",
Payload: { data: "data" }
};
// TODO: mock AWS.Response instance's data attribute with our data.
const cache = {
get: jest.fn(() => {
return Promise.resolve(null);
}),
set: jest.fn()
};
describe("jest", () => {
beforeEach(() => {
AWS.Lambda = function() {
return {
invoke: () => ({
on: (event, callback) => {
if ("undefined" !== typeof callback && event === "success") {
callback(mockData);
}
return {
promise: () => {
return Promise.resolve(mockData);
}
};
}
})
};
};
cache.get.mockClear();
cache.set.mockClear();
});
it("invoke and cache", async () => {
const datasource = new AWSLambdaDataSource(awsOptions, invokeOptions);
datasource.initialize({ cache });
const response = await datasource.invoke(
JSON.stringify({ event: "hello" }),
5
);
expect(response).toEqual(mockData);
expect(cache.set).toBeCalled();
});
it("invoke do not cache", async () => {
const datasource = new AWSLambdaDataSource(awsOptions, invokeOptions);
datasource.initialize({ cache });
const response = await datasource.invoke(
JSON.stringify({ event: "hello" }),
0
);
expect(response).toEqual(mockData);
expect(cache.set).not.toBeCalled();
});
it("invoke do not alter invokeOptions", async () => {
const datasource = new AWSLambdaDataSource(awsOptions, invokeOptions);
datasource.initialize({ cache });
const response = await datasource.invoke(
JSON.stringify({ event: "hello" }),
0
);
expect(datasource.invokeOptions).not.toHaveProperty("Payload");
});
});