-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIterableActionRunner.test.ts
More file actions
98 lines (80 loc) · 2.86 KB
/
Copy pathIterableActionRunner.test.ts
File metadata and controls
98 lines (80 loc) · 2.86 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { IterableActionRunner } from './IterableActionRunner';
import { IterableConfig } from './IterableConfig';
import { IterableActionSource } from '../embedded/types';
describe('IterableActionRunner', () => {
const openAction = { type: 'openUrl', data: 'https://example.com' };
beforeEach(() => {
IterableConfig.urlHandler = null;
IterableConfig.customActionHandler = null;
IterableConfig.openLinksInNewTab = true;
jest.restoreAllMocks();
});
it('should return false when action is null', () => {
const result = IterableActionRunner.executeAction(
null,
null,
IterableActionSource.EMBEDDED
);
expect(result).toBe(false);
});
it('should open URL in new tab by default', () => {
const openSpy = jest.spyOn(window, 'open').mockImplementation();
IterableActionRunner.executeAction(
null,
openAction,
IterableActionSource.EMBEDDED
);
expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank');
});
it('should open URL in same tab when openLinksInNewTab is false', () => {
IterableConfig.openLinksInNewTab = false;
const assignMock = jest.fn();
Object.defineProperty(window, 'location', {
value: { assign: assignMock },
writable: true
});
IterableActionRunner.executeAction(
null,
openAction,
IterableActionSource.EMBEDDED
);
expect(assignMock).toHaveBeenCalledWith('https://example.com');
});
it('should use urlHandler when configured', () => {
const handleIterableURL = jest.fn().mockReturnValue(true);
IterableConfig.urlHandler = { handleIterableURL };
const result = IterableActionRunner.executeAction(
null,
openAction,
IterableActionSource.EMBEDDED
);
expect(result).toBe(true);
expect(handleIterableURL).toHaveBeenCalledWith(
'https://example.com',
expect.objectContaining({ action: openAction })
);
});
it('should fall back to default open when urlHandler returns false', () => {
const handleIterableURL = jest.fn().mockReturnValue(false);
IterableConfig.urlHandler = { handleIterableURL };
const openSpy = jest.spyOn(window, 'open').mockImplementation();
IterableActionRunner.executeAction(
null,
openAction,
IterableActionSource.EMBEDDED
);
expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank');
});
it('should call customActionHandler for non-URL actions', () => {
const handleIterableCustomAction = jest.fn().mockReturnValue(true);
IterableConfig.customActionHandler = { handleIterableCustomAction };
const customAction = { type: 'customType', data: 'someData' };
const result = IterableActionRunner.executeAction(
null,
customAction,
IterableActionSource.EMBEDDED
);
expect(result).toBe(true);
expect(handleIterableCustomAction).toHaveBeenCalled();
});
});