-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprofileAttributeTests.ts
148 lines (129 loc) · 4.05 KB
/
profileAttributeTests.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {ProfileAttributeOperation, Profile} from "../src/actions";
const mockSendToBridge = jest.fn();
jest.doMock("../src/helpers", () => {
const helpers = jest.requireActual("../src/helpers");
// tslint:disable-next-line:only-arrow-functions
return {
...helpers,
sendToBridge: mockSendToBridge,
};
});
import { BatchProfileAttributeEditor } from "../src/modules/profile/profileAttributeEditor";
beforeAll(() => {
mockSendToBridge.mockClear();
});
describe("it enqueues operations correctly", () => {
const editor = new BatchProfileAttributeEditor(true);
const enqueueMock = jest.fn();
(editor as any)._enqueueOperation = enqueueMock;
const oldConsoleLog = console.log;
beforeAll(() => {
console.log = jest.fn();
});
afterAll(() => {
console.log = oldConsoleLog;
});
beforeEach(() => {
enqueueMock.mockClear();
});
it("can set language", () => {
editor.setLanguage("en");
(editor as any).setLanguage(2);
editor.setLanguage(null);
expect(enqueueMock.mock.calls.length).toBe(2);
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetLanguage, {
value: "en",
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetLanguage, {
value: null,
});
});
it("can set region", () => {
editor.setRegion("en");
(editor as any).setRegion(2);
editor.setRegion(null);
expect(enqueueMock.mock.calls.length).toBe(2);
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetRegion, {
value: "en",
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetRegion, {
value: null,
});
});
it("can set attribute", () => {
editor.setAttribute("foo", "bar");
editor.setAttribute("foo", 2);
editor.setAttribute("foo", 2.5);
editor.setAttribute("foo", true);
editor.setAttribute("foo", new Date(1520352788000));
editor.setAttribute("foo", new URL("https://batch.com"));
editor.setAttribute("foo", ["bar", "baz"]);
editor.setAttribute("foo", NaN);
(editor as any).setAttribute(null, null);
(editor as any).setAttribute("foo", null);
expect(enqueueMock.mock.calls.length).toBe(7);
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "string",
value: "bar",
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "integer",
value: 2,
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "float",
value: 2.5,
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "boolean",
value: true,
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "date",
value: 1520352788000,
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "url",
value: "https://batch.com/",
});
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.SetAttribute, {
key: "foo",
type: "array",
value: ["bar", "baz"],
});
});
it("can remove attribute", () => {
editor.removeAttribute("foo");
(editor as any).removeAttribute(null);
expect(enqueueMock.mock.calls.length).toBe(1);
expect(enqueueMock).toBeCalledWith(ProfileAttributeOperation.RemoveAttribute, {
key: "foo",
});
});
});
test("can save operations", () => {
new BatchProfileAttributeEditor(true)
.setAttribute("foo", "bar")
.setAttribute("foo2", ["bar"])
.save();
expect(mockSendToBridge.mock.calls.length).toBe(1);
expect(mockSendToBridge).toBeCalledWith(null, Profile.Edit, [
{
operations: [
{
key: "foo",
operation: ProfileAttributeOperation.SetAttribute,
type: "string",
value: "bar",
},
{ operation: ProfileAttributeOperation.SetAttribute, type: "array", key: "foo2", value: ["bar"] },
],
},
]);
});