@whereby.com/browser-sdk 2.0.0-alpha → 2.0.0-alpha10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -2
- package/dist/lib.cjs +5868 -0
- package/dist/lib.esm.js +5850 -0
- package/dist/types.d.ts +308 -0
- package/dist/v2-alpha10.js +43 -0
- package/package.json +12 -6
- package/.eslintrc +0 -23
- package/.github/actions/build/action.yml +0 -17
- package/.github/workflows/deploy.yml +0 -102
- package/.github/workflows/test.yml +0 -24
- package/.prettierignore +0 -7
- package/.prettierrc +0 -4
- package/.storybook/main.cjs +0 -16
- package/.storybook/preview.js +0 -9
- package/jest.config.js +0 -6
- package/rollup.config.js +0 -70
- package/src/lib/RoomConnection.ts +0 -516
- package/src/lib/RoomParticipant.ts +0 -77
- package/src/lib/__tests__/embed.unit.ts +0 -77
- package/src/lib/api/ApiClient.ts +0 -111
- package/src/lib/api/Credentials.ts +0 -45
- package/src/lib/api/HttpClient.ts +0 -95
- package/src/lib/api/MultipartHttpClient.ts +0 -53
- package/src/lib/api/OrganizationApiClient.ts +0 -64
- package/src/lib/api/Response.ts +0 -34
- package/src/lib/api/credentialsService/index.ts +0 -159
- package/src/lib/api/credentialsService/test/index.spec.ts +0 -181
- package/src/lib/api/deviceService/index.ts +0 -42
- package/src/lib/api/deviceService/tests/index.spec.ts +0 -74
- package/src/lib/api/extractUtils.ts +0 -160
- package/src/lib/api/index.ts +0 -8
- package/src/lib/api/localStorageWrapper/index.ts +0 -15
- package/src/lib/api/models/Account.ts +0 -48
- package/src/lib/api/models/Meeting.ts +0 -42
- package/src/lib/api/models/Organization.ts +0 -186
- package/src/lib/api/models/Room.ts +0 -44
- package/src/lib/api/models/account/EmbeddedFreeTierStatus.ts +0 -34
- package/src/lib/api/models/tests/Account.spec.ts +0 -128
- package/src/lib/api/models/tests/Organization.spec.ts +0 -161
- package/src/lib/api/models/tests/Room.spec.ts +0 -74
- package/src/lib/api/modules/AbstractStore.ts +0 -18
- package/src/lib/api/modules/ChromeStorageStore.ts +0 -44
- package/src/lib/api/modules/LocalStorageStore.ts +0 -57
- package/src/lib/api/modules/tests/ChromeStorageStore.spec.ts +0 -67
- package/src/lib/api/modules/tests/LocalStorageStore.spec.ts +0 -79
- package/src/lib/api/modules/tests/__mocks__/storage.ts +0 -24
- package/src/lib/api/organizationService/index.ts +0 -284
- package/src/lib/api/organizationService/tests/index.spec.ts +0 -781
- package/src/lib/api/organizationServiceCache/index.ts +0 -28
- package/src/lib/api/organizationServiceCache/tests/index.spec.ts +0 -101
- package/src/lib/api/parameterAssertUtils.ts +0 -166
- package/src/lib/api/roomService/index.ts +0 -310
- package/src/lib/api/roomService/tests/index.spec.ts +0 -668
- package/src/lib/api/test/ApiClient.spec.ts +0 -139
- package/src/lib/api/test/HttpClient.spec.ts +0 -120
- package/src/lib/api/test/MultipartHttpClient.spec.ts +0 -145
- package/src/lib/api/test/OrganizationApiClient.spec.ts +0 -132
- package/src/lib/api/test/extractUtils.spec.ts +0 -357
- package/src/lib/api/test/helpers.ts +0 -41
- package/src/lib/api/test/parameterAssertUtils.spec.ts +0 -265
- package/src/lib/api/types.ts +0 -6
- package/src/lib/embed.ts +0 -172
- package/src/lib/index.ts +0 -3
- package/src/lib/react/VideoElement.tsx +0 -16
- package/src/lib/react/index.ts +0 -3
- package/src/lib/react/useLocalMedia.ts +0 -25
- package/src/lib/react/useRoomConnection.ts +0 -92
- package/src/lib/reducer.ts +0 -142
- package/src/stories/custom-ui.stories.tsx +0 -133
- package/src/stories/prebuilt-ui.stories.tsx +0 -131
- package/src/stories/styles.css +0 -74
- package/src/types.d.ts +0 -175
- package/tsconfig.json +0 -30
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import ApiClient from "../ApiClient";
|
|
3
|
-
import Credentials from "../Credentials";
|
|
4
|
-
import Response from "../Response";
|
|
5
|
-
import { itShouldThrowIfInvalid } from "./helpers";
|
|
6
|
-
|
|
7
|
-
jest.mock("axios");
|
|
8
|
-
|
|
9
|
-
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
|
10
|
-
|
|
11
|
-
const baseUrl = "https://localhost:8090";
|
|
12
|
-
const credentials = new Credentials("12345", "67890");
|
|
13
|
-
|
|
14
|
-
describe("ApiClient", () => {
|
|
15
|
-
describe("constructor", () => {
|
|
16
|
-
it("should not throw an error if no constructor params are passed through", () => {
|
|
17
|
-
expect(() => new ApiClient()).not.toThrowError();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
//@ts-expect-error
|
|
21
|
-
itShouldThrowIfInvalid("baseUrl", () => new ApiClient({ baseUrl: null }));
|
|
22
|
-
//@ts-expect-error
|
|
23
|
-
itShouldThrowIfInvalid("fetchDeviceCredentials", () => new ApiClient({ fetchDeviceCredentials: null }));
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe("request", () => {
|
|
27
|
-
let fetchDeviceCredentialsMock: jest.Mock;
|
|
28
|
-
let apiClient: ApiClient;
|
|
29
|
-
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
mockedAxios.request.mockResolvedValue(new Response());
|
|
32
|
-
fetchDeviceCredentialsMock = jest.fn().mockResolvedValueOnce(credentials);
|
|
33
|
-
apiClient = new ApiClient({
|
|
34
|
-
baseUrl,
|
|
35
|
-
fetchDeviceCredentials: fetchDeviceCredentialsMock,
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
afterEach(() => {
|
|
40
|
-
mockedAxios.request.mockClear();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
//@ts-expect-error
|
|
44
|
-
itShouldThrowIfInvalid("url", () => apiClient.request(null, {}));
|
|
45
|
-
|
|
46
|
-
// `url` is required to begin with a `/`
|
|
47
|
-
it('should throw if `url` does not begin with a "/"', () => {
|
|
48
|
-
expect(() => apiClient.request("some-url", {})).toThrowError();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
//@ts-expect-error
|
|
52
|
-
itShouldThrowIfInvalid("fetchDeviceCredentials", () => new ApiClient({ fetchDeviceCredentials: null }));
|
|
53
|
-
|
|
54
|
-
it("should run `this.fetchDeviceCredentials`", () => {
|
|
55
|
-
const url = "/some/path";
|
|
56
|
-
const fetchOptions = {};
|
|
57
|
-
|
|
58
|
-
apiClient.request(url, fetchOptions);
|
|
59
|
-
|
|
60
|
-
expect(fetchDeviceCredentialsMock).toBeCalledTimes(1);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("should return the correct Response value with the correct data if fulfilled", async () => {
|
|
64
|
-
const responseBaseUrl = "www.website.com";
|
|
65
|
-
const url = "/some/path";
|
|
66
|
-
const method = "post";
|
|
67
|
-
const headers = {
|
|
68
|
-
type: "jif",
|
|
69
|
-
};
|
|
70
|
-
const data = { some: "data" };
|
|
71
|
-
const status = 200;
|
|
72
|
-
const statusText = "some status text";
|
|
73
|
-
const response = {
|
|
74
|
-
data,
|
|
75
|
-
headers,
|
|
76
|
-
method,
|
|
77
|
-
status,
|
|
78
|
-
statusText,
|
|
79
|
-
config: {
|
|
80
|
-
url,
|
|
81
|
-
baseURL: responseBaseUrl,
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
const expectedResponse = new Response({
|
|
85
|
-
data,
|
|
86
|
-
headers,
|
|
87
|
-
status: response.status,
|
|
88
|
-
statusText: response.statusText,
|
|
89
|
-
url: `${responseBaseUrl}${url}`,
|
|
90
|
-
});
|
|
91
|
-
mockedAxios.request.mockResolvedValueOnce(response);
|
|
92
|
-
|
|
93
|
-
const result = await apiClient.request(url, {});
|
|
94
|
-
|
|
95
|
-
expect(result).toEqual(expectedResponse);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("should throw an error if rejected with no `response` object", async () => {
|
|
99
|
-
const url = "/some/path";
|
|
100
|
-
const data = {};
|
|
101
|
-
mockedAxios.request.mockRejectedValue(new Response({ status: 404, data }));
|
|
102
|
-
|
|
103
|
-
await expect(apiClient.request(url, {})).rejects.toThrow("Could not make the request.");
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("should return the correct Response value with the correct data if rejected", async () => {
|
|
107
|
-
const responseBaseUrl = "www.website.com";
|
|
108
|
-
const url = "/some/path";
|
|
109
|
-
const method = "post";
|
|
110
|
-
const headers = {
|
|
111
|
-
type: "jif",
|
|
112
|
-
};
|
|
113
|
-
const data = { some: "data" };
|
|
114
|
-
const status = 200;
|
|
115
|
-
const statusText = "some status text";
|
|
116
|
-
const response = {
|
|
117
|
-
data,
|
|
118
|
-
headers,
|
|
119
|
-
method,
|
|
120
|
-
status,
|
|
121
|
-
statusText,
|
|
122
|
-
config: {
|
|
123
|
-
url,
|
|
124
|
-
baseURL: responseBaseUrl,
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
const expectedResponse = new Response({
|
|
128
|
-
data,
|
|
129
|
-
headers,
|
|
130
|
-
status: response.status,
|
|
131
|
-
statusText: response.statusText,
|
|
132
|
-
url: `${responseBaseUrl}${url}`,
|
|
133
|
-
});
|
|
134
|
-
mockedAxios.request.mockRejectedValue({ response });
|
|
135
|
-
|
|
136
|
-
await expect(apiClient.request(url, {})).rejects.toEqual(expectedResponse);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
});
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import HttpClient from "../HttpClient";
|
|
3
|
-
import Response from "../Response";
|
|
4
|
-
import { itShouldThrowIfInvalid } from "./helpers";
|
|
5
|
-
|
|
6
|
-
jest.mock("axios");
|
|
7
|
-
|
|
8
|
-
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
|
9
|
-
|
|
10
|
-
const baseUrl = "https://localhost:8090";
|
|
11
|
-
|
|
12
|
-
describe("HttpClient", () => {
|
|
13
|
-
describe("constructor", () => {
|
|
14
|
-
it("should set `baseUrl` param as class properties", () => {
|
|
15
|
-
const httpClient = new HttpClient({
|
|
16
|
-
baseUrl,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
expect(httpClient._baseUrl).toBe(baseUrl);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
//@ts-expect-error
|
|
23
|
-
itShouldThrowIfInvalid("baseUrl", () => new HttpClient({ baseUrl: null }));
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe("request", () => {
|
|
27
|
-
let httpClient: HttpClient;
|
|
28
|
-
|
|
29
|
-
beforeEach(() => {
|
|
30
|
-
httpClient = new HttpClient({
|
|
31
|
-
baseUrl,
|
|
32
|
-
});
|
|
33
|
-
mockedAxios.request.mockClear();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
//@ts-expect-error
|
|
37
|
-
itShouldThrowIfInvalid("url", () => httpClient.request(null, {}));
|
|
38
|
-
|
|
39
|
-
// `url` is required to begin with a `/`
|
|
40
|
-
it('should throw if `url` does not begin with a "/"', () => {
|
|
41
|
-
expect(() => httpClient.request("some-url", {})).toThrowError();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("should return the correct Response value with the correct data if fulfilled", async () => {
|
|
45
|
-
const responseBaseUrl = "www.website.com";
|
|
46
|
-
const url = "/some/path";
|
|
47
|
-
const method = "post";
|
|
48
|
-
const headers = {
|
|
49
|
-
type: "jif",
|
|
50
|
-
};
|
|
51
|
-
const data = { some: "data" };
|
|
52
|
-
const status = 200;
|
|
53
|
-
const statusText = "some status text";
|
|
54
|
-
const response = {
|
|
55
|
-
data,
|
|
56
|
-
headers,
|
|
57
|
-
method,
|
|
58
|
-
status,
|
|
59
|
-
statusText,
|
|
60
|
-
config: {
|
|
61
|
-
url,
|
|
62
|
-
baseURL: responseBaseUrl,
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
const expectedResponse = new Response({
|
|
66
|
-
data,
|
|
67
|
-
headers,
|
|
68
|
-
status: response.status,
|
|
69
|
-
statusText: response.statusText,
|
|
70
|
-
url: `${responseBaseUrl}${url}`,
|
|
71
|
-
});
|
|
72
|
-
mockedAxios.request.mockResolvedValue(response);
|
|
73
|
-
|
|
74
|
-
const result = await httpClient.request(url, {});
|
|
75
|
-
|
|
76
|
-
expect(result).toEqual(expectedResponse);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("should throw an error if rejected with no `response` object", async () => {
|
|
80
|
-
const url = "/some/path";
|
|
81
|
-
const data = {};
|
|
82
|
-
mockedAxios.request.mockRejectedValue(new Response({ status: 404, data }));
|
|
83
|
-
|
|
84
|
-
await expect(httpClient.request(url, {})).rejects.toThrow("Could not make the request.");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("should return the correct Response value with the correct data if rejected", async () => {
|
|
88
|
-
const responseBaseUrl = "www.website.com";
|
|
89
|
-
const url = "/some/path";
|
|
90
|
-
const method = "post";
|
|
91
|
-
const headers = {
|
|
92
|
-
type: "jif",
|
|
93
|
-
};
|
|
94
|
-
const data = { some: "data" };
|
|
95
|
-
const status = 200;
|
|
96
|
-
const statusText = "some status text";
|
|
97
|
-
const response = {
|
|
98
|
-
data,
|
|
99
|
-
headers,
|
|
100
|
-
method,
|
|
101
|
-
status,
|
|
102
|
-
statusText,
|
|
103
|
-
config: {
|
|
104
|
-
url,
|
|
105
|
-
baseURL: responseBaseUrl,
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
const expectedResponse = new Response({
|
|
109
|
-
data,
|
|
110
|
-
headers,
|
|
111
|
-
status: response.status,
|
|
112
|
-
statusText: response.statusText,
|
|
113
|
-
url: `${responseBaseUrl}${url}`,
|
|
114
|
-
});
|
|
115
|
-
mockedAxios.request.mockRejectedValue({ response });
|
|
116
|
-
|
|
117
|
-
await expect(httpClient.request(url, {})).rejects.toEqual(expectedResponse);
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
});
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
import MultipartHttpClient from "../MultipartHttpClient";
|
|
4
|
-
import Response from "../Response";
|
|
5
|
-
import HttpClient from "../HttpClient";
|
|
6
|
-
import { itShouldThrowIfInvalid } from "./helpers";
|
|
7
|
-
|
|
8
|
-
jest.mock("axios");
|
|
9
|
-
|
|
10
|
-
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
|
11
|
-
|
|
12
|
-
class FormData {
|
|
13
|
-
values: Record<string, string>;
|
|
14
|
-
constructor() {
|
|
15
|
-
this.values = {};
|
|
16
|
-
}
|
|
17
|
-
append(key, value) {
|
|
18
|
-
this.values[key] = value;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
describe("MultipartHttpClient", () => {
|
|
23
|
-
describe("dataToFormData", () => {
|
|
24
|
-
let originalFormData;
|
|
25
|
-
beforeAll(() => {
|
|
26
|
-
originalFormData = global.FormData;
|
|
27
|
-
global.FormData = FormData;
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
afterAll(() => {
|
|
31
|
-
global.FormData = originalFormData;
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
itShouldThrowIfInvalid("data", () => {
|
|
35
|
-
MultipartHttpClient.dataToFormData(null);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("should return empty FormData if provided object is empty", () => {
|
|
39
|
-
const formData = MultipartHttpClient.dataToFormData({});
|
|
40
|
-
|
|
41
|
-
expect(formData).toEqual(new FormData());
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("should return FormData where each key and value is added", () => {
|
|
45
|
-
const expectedFormData = new FormData();
|
|
46
|
-
expectedFormData.append("a", "some a");
|
|
47
|
-
expectedFormData.append("some other key", "some other key value");
|
|
48
|
-
|
|
49
|
-
const formData = MultipartHttpClient.dataToFormData({
|
|
50
|
-
a: "some a",
|
|
51
|
-
"some other key": "some other key value",
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
expect(formData).toEqual(expectedFormData);
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe("constructor", () => {
|
|
59
|
-
itShouldThrowIfInvalid("httpClient", () => new MultipartHttpClient({ httpClient: undefined }));
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
describe("request", () => {
|
|
63
|
-
let httpClient: HttpClient;
|
|
64
|
-
let multipartHttpClient: MultipartHttpClient;
|
|
65
|
-
const uri = "/api";
|
|
66
|
-
const baseUrl = "https://localhost:8090";
|
|
67
|
-
|
|
68
|
-
beforeEach(() => {
|
|
69
|
-
httpClient = new HttpClient({
|
|
70
|
-
baseUrl,
|
|
71
|
-
});
|
|
72
|
-
mockedAxios.request.mockClear();
|
|
73
|
-
multipartHttpClient = new MultipartHttpClient({ httpClient });
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("should reject with the response from httpClient if rejected", async () => {
|
|
77
|
-
const response = new Response({ status: 400 });
|
|
78
|
-
mockedAxios.request.mockRejectedValueOnce(response);
|
|
79
|
-
|
|
80
|
-
await expect(multipartHttpClient.request(uri, { method: "GET" })).rejects.toThrow(
|
|
81
|
-
"Could not make the request."
|
|
82
|
-
);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("should resolve with the response from httpClient if resolved", async () => {
|
|
86
|
-
const response = new Response({ status: 204 });
|
|
87
|
-
mockedAxios.request.mockResolvedValueOnce(response);
|
|
88
|
-
|
|
89
|
-
const result = await multipartHttpClient.request(uri, { method: "GET" });
|
|
90
|
-
|
|
91
|
-
expect(result).toEqual(response);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("should call httpClient with merged options object that handles form parameters", async () => {
|
|
95
|
-
const data = {
|
|
96
|
-
someField: "someValue",
|
|
97
|
-
};
|
|
98
|
-
const options = {
|
|
99
|
-
method: "POST",
|
|
100
|
-
data,
|
|
101
|
-
headers: {
|
|
102
|
-
accept: "text/xml",
|
|
103
|
-
"Content-Type": "text/xml",
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
const response = new Response({ status: 204 });
|
|
107
|
-
mockedAxios.request.mockResolvedValueOnce(response);
|
|
108
|
-
|
|
109
|
-
await multipartHttpClient.request(uri, options);
|
|
110
|
-
|
|
111
|
-
expect(mockedAxios.request).toBeCalledWith({
|
|
112
|
-
baseURL: baseUrl,
|
|
113
|
-
method: "POST",
|
|
114
|
-
data,
|
|
115
|
-
headers: {
|
|
116
|
-
accept: "text/xml",
|
|
117
|
-
"Content-Type": undefined,
|
|
118
|
-
},
|
|
119
|
-
transformRequest: MultipartHttpClient.dataToFormData,
|
|
120
|
-
url: uri,
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("should call httpClient with contentType undefined even if it is provided", async () => {
|
|
125
|
-
const contentType = "overridenValue";
|
|
126
|
-
const response = new Response({ status: 204 });
|
|
127
|
-
mockedAxios.request.mockResolvedValueOnce(response);
|
|
128
|
-
|
|
129
|
-
await multipartHttpClient.request(uri, {
|
|
130
|
-
headers: {
|
|
131
|
-
"Content-Type": contentType,
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
expect(mockedAxios.request).toBeCalledWith({
|
|
136
|
-
baseURL: baseUrl,
|
|
137
|
-
headers: {
|
|
138
|
-
"Content-Type": undefined,
|
|
139
|
-
},
|
|
140
|
-
transformRequest: MultipartHttpClient.dataToFormData,
|
|
141
|
-
url: uri,
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
});
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import ApiClient from "../ApiClient";
|
|
2
|
-
import Response from "../Response";
|
|
3
|
-
import OrganizationApiClient from "../OrganizationApiClient";
|
|
4
|
-
import { itShouldThrowIfInvalid } from "./helpers";
|
|
5
|
-
import { HttpClientRequestConfig } from "../HttpClient";
|
|
6
|
-
|
|
7
|
-
jest.mock("../ApiClient");
|
|
8
|
-
|
|
9
|
-
describe("OrganizationApiClient", () => {
|
|
10
|
-
let apiClient: jest.Mocked<ApiClient>;
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
apiClient = new ApiClient() as jest.Mocked<ApiClient>;
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
describe("constructor", () => {
|
|
17
|
-
//@ts-expect-error
|
|
18
|
-
itShouldThrowIfInvalid("apiClient", () => new OrganizationApiClient({ apiClient: null }));
|
|
19
|
-
itShouldThrowIfInvalid(
|
|
20
|
-
"fetchOrganization",
|
|
21
|
-
() =>
|
|
22
|
-
new OrganizationApiClient({
|
|
23
|
-
apiClient,
|
|
24
|
-
//@ts-expect-error
|
|
25
|
-
fetchOrganization: null,
|
|
26
|
-
})
|
|
27
|
-
);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
function testRequestMethod(requestMethodName: "request" | "requestMultipart") {
|
|
31
|
-
describe(requestMethodName, () => {
|
|
32
|
-
let fetchOrganization: jest.Mock;
|
|
33
|
-
let organizationApiClient: OrganizationApiClient;
|
|
34
|
-
let apiRequestMethod: jest.MockInstance<Promise<Response>, [url: string, options: HttpClientRequestConfig]>;
|
|
35
|
-
let organizationApiRequestMethod: (
|
|
36
|
-
url: string,
|
|
37
|
-
config: HttpClientRequestConfig
|
|
38
|
-
) => Promise<Response>;
|
|
39
|
-
|
|
40
|
-
beforeEach(() => {
|
|
41
|
-
fetchOrganization = jest.fn();
|
|
42
|
-
organizationApiClient = new OrganizationApiClient({
|
|
43
|
-
apiClient,
|
|
44
|
-
fetchOrganization,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
apiRequestMethod = apiClient[requestMethodName];
|
|
48
|
-
|
|
49
|
-
organizationApiRequestMethod = organizationApiClient[requestMethodName].bind(organizationApiClient);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
//@ts-expect-error
|
|
53
|
-
itShouldThrowIfInvalid("url", () => organizationApiRequestMethod(null, {}));
|
|
54
|
-
|
|
55
|
-
// `url` is required to begin with a `/`
|
|
56
|
-
it('should throw if `url` does not begin with a "/"', () => {
|
|
57
|
-
expect(() => organizationApiRequestMethod("some-url", {})).toThrowError();
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("should run `this.fetchOrganization`", () => {
|
|
61
|
-
const url = "/some/path";
|
|
62
|
-
const fetchOptions = {};
|
|
63
|
-
fetchOrganization.mockResolvedValue(null);
|
|
64
|
-
|
|
65
|
-
organizationApiRequestMethod(url, fetchOptions);
|
|
66
|
-
|
|
67
|
-
expect(fetchOrganization).toBeCalledTimes(1);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe("when no organization is returned", () => {
|
|
71
|
-
beforeEach(() => {
|
|
72
|
-
fetchOrganization.mockResolvedValue(null);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it("should call the apiClient without organization prefix", () => {
|
|
76
|
-
const url = "/some/path";
|
|
77
|
-
const opts = { headers: { blah: "blah" } };
|
|
78
|
-
apiRequestMethod.mockResolvedValue(new Response());
|
|
79
|
-
|
|
80
|
-
const promise = organizationApiRequestMethod(url, opts);
|
|
81
|
-
|
|
82
|
-
return promise.then(() => {
|
|
83
|
-
expect(apiRequestMethod).toBeCalledWith(url, opts);
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it("should return the response from the apiClient", async () => {
|
|
88
|
-
const url = "/some/path";
|
|
89
|
-
const options = { headers: { blah: "blah" } };
|
|
90
|
-
const response = new Response({ status: 204 });
|
|
91
|
-
apiRequestMethod.mockResolvedValue(response);
|
|
92
|
-
|
|
93
|
-
const result = await organizationApiRequestMethod(url, options);
|
|
94
|
-
|
|
95
|
-
expect(result).toEqual(response);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe("when an organization is returned", () => {
|
|
100
|
-
const organizationId = 707;
|
|
101
|
-
const options = { headers: { blah: "blah" } };
|
|
102
|
-
beforeEach(() => {
|
|
103
|
-
fetchOrganization.mockResolvedValue({ organizationId });
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("should call the apiClient without organization prefix", () => {
|
|
107
|
-
const url = "/some/path";
|
|
108
|
-
apiRequestMethod.mockResolvedValue(new Response());
|
|
109
|
-
|
|
110
|
-
const promise = organizationApiRequestMethod(url, options);
|
|
111
|
-
|
|
112
|
-
return promise.then(() => {
|
|
113
|
-
expect(apiRequestMethod).toBeCalledWith(`/organizations/${organizationId}` + url, options);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("should return the response from the apiClient", async () => {
|
|
118
|
-
const url = "/some/path";
|
|
119
|
-
const response = new Response({ status: 204 });
|
|
120
|
-
apiRequestMethod.mockResolvedValue(response);
|
|
121
|
-
|
|
122
|
-
const result = await organizationApiRequestMethod(url, options);
|
|
123
|
-
|
|
124
|
-
expect(result).toEqual(response);
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
testRequestMethod("request");
|
|
131
|
-
testRequestMethod("requestMultipart");
|
|
132
|
-
});
|