Skip to content

Commit b22a5e1

Browse files
committed
Unit test ServiceUtils #configure and #configureCultureCode
1 parent 1878a02 commit b22a5e1

1 file changed

Lines changed: 86 additions & 2 deletions

File tree

src/utilities/service-utils.test.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import faker from "faker";
2+
import { LocalizationUtils } from "./localization-utils";
13
import { ServiceUtils } from "./service-utils";
24
import { StubResourceRecord } from "../tests/stubs/stub-resource-record";
35
import { Factory } from "rosie";
46
import { AxiosResponse } from "axios";
57
import { FactoryType } from "../tests/factories/factory-type";
68
import { FactoryType as AndcultureCodeFactoryType } from "andculturecode-javascript-testing";
79
import { ResultRecord } from "../view-models/result-record";
10+
import axios from "axios";
811
import "jest-extended";
912

1013
describe("ServiceUtils", () => {
@@ -13,7 +16,59 @@ describe("ServiceUtils", () => {
1316
// -----------------------------------------------------------------------------------------
1417

1518
describe("configure", () => {
16-
test.skip("TODO", () => {});
19+
test.each`
20+
cultureCode
21+
${null}
22+
${undefined}
23+
${""}
24+
${" "}
25+
`(
26+
"when cultureCode of $cultureCode, configures baseUrl with default culture code",
27+
({ cultureCode }) => {
28+
// Arrange
29+
const expected = LocalizationUtils.defaultCultureCode().toLowerCase();
30+
31+
// Act
32+
ServiceUtils.configure(cultureCode);
33+
34+
// Assert
35+
expect(axios.defaults.baseURL).toContain(expected);
36+
}
37+
);
38+
39+
test("when cultureCode set, configures baseUrl with provided value", () => {
40+
// Arrange
41+
const expected = faker.random.locale();
42+
43+
// Act
44+
ServiceUtils.configure(expected);
45+
46+
// Assert
47+
expect(axios.defaults.baseURL).toContain(expected.toLowerCase());
48+
});
49+
50+
test("when API response handlers set, configures axio response interceptors", () => {
51+
// Arrange
52+
const expectedErrorHandler = (): any => {};
53+
const expectedSuccessHandler = (): any => {};
54+
const interceptorSpy = jest.spyOn(
55+
axios.interceptors.response,
56+
"use"
57+
);
58+
59+
// Act
60+
ServiceUtils.configure(
61+
faker.random.locale(),
62+
expectedErrorHandler,
63+
expectedSuccessHandler
64+
);
65+
66+
// Assert
67+
expect(interceptorSpy).toHaveBeenCalledWith(
68+
expectedSuccessHandler,
69+
expectedErrorHandler
70+
);
71+
});
1772
});
1873

1974
//#endregion configure
@@ -23,7 +78,36 @@ describe("ServiceUtils", () => {
2378
// -----------------------------------------------------------------------------------------
2479

2580
describe("configureCultureCode", () => {
26-
test.skip("TODO", () => {});
81+
test.each`
82+
cultureCode
83+
${null}
84+
${undefined}
85+
${""}
86+
${" "}
87+
`(
88+
"when cultureCode of $cultureCode, configures baseUrl with default culture code",
89+
({ cultureCode }) => {
90+
// Arrange
91+
const expected = LocalizationUtils.defaultCultureCode().toLowerCase();
92+
93+
// Act
94+
ServiceUtils.configureCultureCode(cultureCode);
95+
96+
// Assert
97+
expect(axios.defaults.baseURL).toContain(expected);
98+
}
99+
);
100+
101+
test("when cultureCode set, configures baseUrl with provided value", () => {
102+
// Arrange
103+
const expected = faker.random.locale();
104+
105+
// Act
106+
ServiceUtils.configureCultureCode(expected);
107+
108+
// Assert
109+
expect(axios.defaults.baseURL).toContain(expected.toLowerCase());
110+
});
27111
});
28112

29113
//#endregion configureCultureCode

0 commit comments

Comments
 (0)