Skip to content

Commit aa01a09

Browse files
committed
update tests
1 parent c9919b1 commit aa01a09

File tree

3 files changed

+195
-62
lines changed

3 files changed

+195
-62
lines changed

src/__tests__/getCurrentConnectionFromPortal.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('getCurrentConnectionFromPortal', () => {
1717
getDesignerProject: jest.fn(),
1818
getDesignerProjects: jest.fn(),
1919
listConnections: jest.fn(),
20+
listOrganizations: jest.fn(),
2021
listVoiceboxConversations: jest.fn(),
2122
profile: jest.fn(),
2223
renameDesignerProject: jest.fn(),

src/__tests__/getCurrentConnectionInfo.test.ts

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getCurrentConnectionInfo } from '../getCurrentConnectionInfo';
21
import { DEMO_CONNECTION_INDEX } from '../cookies';
2+
import { getCurrentConnectionInfo } from '../getCurrentConnectionInfo';
33

44
const getCurrentConnectionInfoForPathname = (pathname: string) => {
55
const location = {
@@ -17,52 +17,82 @@ const getCurrentConnectionInfoForPathname = (pathname: string) => {
1717
};
1818

1919
describe('getCurrentConnectionInfo', () => {
20-
it('returns the correct connectionIndex and product for a valid url', () => {
21-
const info = getCurrentConnectionInfoForPathname('/u/9/explorer/');
22-
expect(info).toBeTruthy();
23-
expect(info?.connectionIndex).toBe(9);
24-
expect(info?.product).toBe('explorer');
25-
expect(info?.productPath).toBe(undefined);
26-
});
20+
describe.each([
21+
['personal org', '/u', undefined],
22+
['public org', '/o/stardog', 'stardog'],
23+
])('for %s', (label, prefix, orgDomain) => {
24+
it('returns the correct organizationDomain, connectionIndex and product for a valid url', () => {
25+
const info = getCurrentConnectionInfoForPathname(`${prefix}/9/explorer/`);
26+
expect(info).toBeTruthy();
27+
expect(info?.connectionIndex).toBe(9);
28+
expect(info?.organizationDomain).toBe(orgDomain);
29+
expect(info?.product).toBe('explorer');
30+
expect(info?.productPath).toBe(undefined);
31+
});
2732

28-
it('returns the demo connectionIndex for a valid demo url', () => {
29-
const info = getCurrentConnectionInfoForPathname('/u/demo/explorer/');
30-
expect(info).toBeTruthy();
31-
expect(info?.connectionIndex).toBe(DEMO_CONNECTION_INDEX);
32-
expect(info?.product).toBe('explorer');
33-
expect(info?.productPath).toBe(undefined);
34-
});
33+
it('returns the demo connectionIndex for a valid personal demo url', () => {
34+
const info = getCurrentConnectionInfoForPathname(
35+
`${prefix}/demo/explorer/`
36+
);
37+
expect(info).toBeTruthy();
38+
expect(info?.connectionIndex).toBe(DEMO_CONNECTION_INDEX);
39+
expect(info?.organizationDomain).toBe(orgDomain);
40+
expect(info?.product).toBe('explorer');
41+
expect(info?.productPath).toBe(undefined);
42+
});
3543

36-
it('returns the correct productPath for a valid url', () => {
37-
const info = getCurrentConnectionInfoForPathname(
38-
'/u/9/explorer/unstable/index.html'
39-
);
40-
expect(info).toBeTruthy();
41-
expect(info?.connectionIndex).toBe(9);
42-
expect(info?.product).toBe('explorer');
43-
expect(info?.productPath).toBe('unstable/index.html');
44-
});
44+
it('returns the correct productPath for a valid url', () => {
45+
const info = getCurrentConnectionInfoForPathname(
46+
`${prefix}/9/explorer/unstable/index.html`
47+
);
48+
expect(info).toBeTruthy();
49+
expect(info?.connectionIndex).toBe(9);
50+
expect(info?.organizationDomain).toBe(orgDomain);
51+
expect(info?.product).toBe('explorer');
52+
expect(info?.productPath).toBe('unstable/index.html');
53+
});
4554

46-
it('returns correct info for missing-product url', () => {
47-
const info = getCurrentConnectionInfoForPathname('/u/7/');
48-
expect(info).toBeTruthy();
49-
expect(info?.connectionIndex).toBe(7);
50-
expect(info?.product).toBe(undefined);
51-
});
55+
it('returns correct info for missing-product url', () => {
56+
const info = getCurrentConnectionInfoForPathname(`${prefix}/7/`);
57+
expect(info).toBeTruthy();
58+
expect(info?.connectionIndex).toBe(7);
59+
expect(info?.organizationDomain).toBe(orgDomain);
60+
expect(info?.product).toBe(undefined);
61+
});
5262

53-
it('returns correct info for missing-product url without slash at the end', () => {
54-
const info = getCurrentConnectionInfoForPathname('/u/7');
55-
expect(info).toBeTruthy();
56-
expect(info?.connectionIndex).toBe(7);
57-
expect(info?.product).toBe(undefined);
63+
it('returns correct info for missing-product url without slash at the end', () => {
64+
const info = getCurrentConnectionInfoForPathname(`${prefix}/7`);
65+
expect(info).toBeTruthy();
66+
expect(info?.connectionIndex).toBe(7);
67+
expect(info?.organizationDomain).toBe(orgDomain);
68+
expect(info?.product).toBe(undefined);
69+
});
5870
});
5971

6072
it('returns null for invalid paths', () => {
6173
[
6274
'/invalid/u/0/product',
6375
'/invalid/u/0/',
76+
// invalid connection
77+
'/o/stardog/invalid/product',
78+
'/o/stardog/invalid/',
79+
'/o/stardog/invalid',
80+
'/o/stardog/',
81+
'/o/stardog',
82+
// invalid org
83+
'/o/123/demo/product',
84+
'/o/123/demo/',
85+
'/o/123/demo',
86+
'/o/123/',
87+
'/o/123',
88+
// invalid connection
6489
'/u/invalid/product',
90+
'/u/invalid/',
6591
'/u/invalid',
92+
// incomplete path
93+
'/o/',
94+
'/o',
95+
'/u/',
6696
'/u',
6797
'/',
6898
].forEach((pathname) => {

src/__tests__/getPortalSdk.test.ts

Lines changed: 129 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getConnectionCookie } from '../cookies';
2+
import { getCurrentConnectionInfo } from '../getCurrentConnectionInfo';
23
import {
34
ClientTypeList,
45
TrackingEventList,
@@ -14,16 +15,24 @@ jest.mock('../cookies', () => ({
1415
getConnectionCookie: jest.fn(),
1516
}));
1617

17-
const addShare = jest.fn(async () => ({ addShare: null }));
18-
const getConnectionByIndex = jest.fn(async () => ({ connection: null }));
19-
const createDesignerProject = jest.fn(async () => ({
20-
createDesignerProject: 'id',
18+
jest.mock('../getCurrentConnectionInfo', () => ({
19+
getCurrentConnectionInfo: jest.fn(),
2120
}));
21+
22+
const addShare = jest.fn(async () => ({ addShare: null }));
2223
const archiveDesignerProject = jest.fn(async () => ({
2324
archiveDesignerProject: 'id',
2425
}));
25-
const restoreDesignerProject = jest.fn(async () => ({
26-
restoreDesignerProject: 'id',
26+
const createDesignerProject = jest.fn(async () => ({
27+
createDesignerProject: 'id',
28+
}));
29+
const getConnectionByIndex = jest.fn(async () => ({
30+
connection: {
31+
endpoint: 'endpoint',
32+
id: 'id',
33+
name: 'name',
34+
index: 0,
35+
},
2736
}));
2837
const getDesignerProject = jest.fn(async () => {
2938
return {
@@ -43,6 +52,7 @@ const getVoiceboxConversation = jest.fn(async () => ({
4352
getVoiceboxConversation: null,
4453
}));
4554
const listConnections = jest.fn(async () => ({ listConnections: [null] }));
55+
const listOrganizations = jest.fn(async () => ({ listOrganizations: [null] }));
4656
const listVoiceboxConversations = jest.fn(async () => ({
4757
listVoiceboxConversations: null,
4858
voiceboxConversationCount: null,
@@ -51,6 +61,9 @@ const profile = jest.fn(async () => ({ profile: null }));
5161
const renameDesignerProject = jest.fn(async () => ({
5262
renameDesignerProject: 'id',
5363
}));
64+
const restoreDesignerProject = jest.fn(async () => ({
65+
restoreDesignerProject: 'id',
66+
}));
5467
const trackEvent = jest.fn(async () => ({ trackEvent: null }));
5568
const updateDesignerProject = jest.fn(async () => ({
5669
updateDesignerProject: 'id',
@@ -60,13 +73,14 @@ describe('getPortalSdk', () => {
6073
beforeEach(() => {
6174
jest.spyOn(portalSdkImport, 'getSdk').mockReturnValue({
6275
addShare,
63-
getConnectionByIndex,
64-
createDesignerProject,
6576
archiveDesignerProject,
77+
createDesignerProject,
78+
getConnectionByIndex,
6679
getDesignerProject,
6780
getDesignerProjects,
6881
getVoiceboxConversation,
6982
listConnections,
83+
listOrganizations,
7084
listVoiceboxConversations,
7185
profile,
7286
renameDesignerProject,
@@ -102,34 +116,122 @@ describe('getPortalSdk', () => {
102116

103117
it('calls portal-sdk functions and gets return values', async () => {
104118
const sdk = getPortalSdk();
105-
const input = {
106-
endpoint: 'endpoint',
107-
expires: 100,
108-
service: 'service',
109-
target_path: 'target_path',
119+
120+
const addShareInput = {
121+
input: {
122+
endpoint: 'endpoint',
123+
expires: 100,
124+
service: 'service',
125+
target_path: 'target_path',
126+
},
110127
};
111-
await sdk?.addShare(input);
112-
expect(addShare).toHaveBeenCalledWith({ input });
113-
const eventInput = {
114-
event: TrackingEventList.DESIGNER_CREATE_DATA_SOURCE,
115-
client_type: ClientTypeList.HUBSPOT,
128+
await sdk?.addShare(addShareInput.input);
129+
expect(addShare).toHaveBeenCalledWith(addShareInput);
130+
131+
const createDesignerProjectInput = {
132+
name: 'name',
133+
content: 'content',
134+
connection_id: 'connectionId',
116135
};
117-
await sdk?.trackEvent(eventInput);
118-
expect(trackEvent).toHaveBeenCalled();
136+
await sdk?.createDesignerProject(
137+
createDesignerProjectInput.name,
138+
createDesignerProjectInput.content,
139+
createDesignerProjectInput.connection_id
140+
);
141+
expect(createDesignerProject).toHaveBeenCalledWith(
142+
createDesignerProjectInput
143+
);
144+
145+
const archiveDesignerProjectInput = { project_id: 'projectId' };
146+
await sdk?.archiveDesignerProject(archiveDesignerProjectInput.project_id);
147+
expect(archiveDesignerProject).toHaveBeenCalledWith(
148+
archiveDesignerProjectInput
149+
);
150+
151+
const getDesignerProjectInput = { project_id: 'projectId' };
152+
await sdk?.getDesignerProject(getDesignerProjectInput.project_id);
153+
expect(getDesignerProject).toHaveBeenCalledWith(getDesignerProjectInput);
154+
155+
await sdk?.getDesignerProjects();
156+
expect(getDesignerProjects).toHaveBeenCalled();
157+
158+
const getVoiceboxConversationInput = { conversation_id: 'conversationId' };
159+
await sdk?.getVoiceboxConversation(
160+
getVoiceboxConversationInput.conversation_id
161+
);
162+
expect(getVoiceboxConversation).toHaveBeenCalledWith(
163+
getVoiceboxConversationInput
164+
);
165+
await sdk?.listOrganizations();
166+
expect(listOrganizations).toHaveBeenCalled();
167+
168+
await sdk?.listVoiceboxConversations();
169+
expect(listVoiceboxConversations).toHaveBeenCalled();
119170

120171
await sdk?.profile();
121172
expect(profile).toHaveBeenCalled();
122173

123-
await sdk?.listConnections();
124-
expect(listConnections).toHaveBeenCalled();
174+
const renameDesignerProjectInput = {
175+
project_id: 'projectId',
176+
name: 'name',
177+
};
178+
await sdk?.renameDesignerProject(
179+
renameDesignerProjectInput.project_id,
180+
renameDesignerProjectInput.name
181+
);
182+
expect(renameDesignerProject).toHaveBeenCalledWith(
183+
renameDesignerProjectInput
184+
);
185+
186+
const restoreDesignerProjectInput = { project_id: 'projectId' };
187+
await sdk?.restoreDesignerProject(restoreDesignerProjectInput.project_id);
188+
expect(restoreDesignerProject).toHaveBeenCalledWith(
189+
restoreDesignerProjectInput
190+
);
125191

126-
await sdk?.getConnectionByIndex(0);
127-
expect(getConnectionByIndex).toHaveBeenCalledWith({ index: 0 });
192+
await sdk?.trackEvent({
193+
event: TrackingEventList.DESIGNER_CREATE_DATA_SOURCE,
194+
client_type: ClientTypeList.HUBSPOT,
195+
});
196+
expect(trackEvent).toHaveBeenCalled();
197+
198+
const updateDesignerProjectInput = {
199+
project_id: 'projectId',
200+
content: 'content',
201+
name: 'name',
202+
connection_id: 'connectionId',
203+
};
204+
await sdk?.updateDesignerProject(
205+
updateDesignerProjectInput.project_id,
206+
updateDesignerProjectInput.content,
207+
updateDesignerProjectInput.name,
208+
updateDesignerProjectInput.connection_id
209+
);
210+
expect(updateDesignerProject).toHaveBeenCalledWith(
211+
updateDesignerProjectInput
212+
);
213+
});
214+
215+
it('calls the connection portal-sdk function only when there is an org domain', async () => {
216+
const sdk = getPortalSdk();
217+
218+
(getCurrentConnectionInfo as jest.Mock).mockReturnValue(null);
219+
220+
expect(await sdk?.getConnectionByIndex(0)).toBeNull();
221+
expect(getConnectionByIndex).not.toHaveBeenCalled();
222+
223+
expect(await sdk?.listConnections()).toBeNull();
224+
expect(listConnections).not.toHaveBeenCalled();
225+
226+
(getCurrentConnectionInfo as jest.Mock).mockReturnValue({
227+
connectionIndex: 1,
228+
organizationDomain: 'orgDomain',
229+
});
128230

129-
listConnections.mockResolvedValueOnce({ listConnections: null as any });
130-
const emptyConnectionsSdk = getPortalSdk();
231+
expect(await sdk?.getConnectionByIndex(0)).not.toBeNull();
232+
expect(getConnectionByIndex).toHaveBeenCalled();
131233

132-
await emptyConnectionsSdk?.listConnections();
234+
expect(await sdk?.listConnections()).not.toBeNull();
133235
expect(listConnections).toHaveBeenCalled();
134236
});
135237
});

0 commit comments

Comments
 (0)