This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from indykite/IK-1761
Unit tests and some fixes
- Loading branch information
Showing
52 changed files
with
12,522 additions
and
2,782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
# misc | ||
.DS_Store | ||
.idea/ | ||
.vscode/launch.json | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
const { | ||
getBaseUri, | ||
getApplicationId, | ||
getTenantId, | ||
getLocaleConfig, | ||
getBaseAuthUrl, | ||
} = require("../config"); | ||
const { enUSLocale } = require("../../../../services/core/locale/en-US"); | ||
|
||
const originalConsole = window.console; | ||
|
||
beforeEach(() => { | ||
window.IKSDK = { | ||
config: { | ||
baseUri: "http://www.example.com", | ||
applicationId: "12345", | ||
tenantId: "tenant-id", | ||
localeConfig: { | ||
locale: "sk-SK", | ||
messages: {}, | ||
}, | ||
}, | ||
}; | ||
|
||
window.console = { | ||
error: jest.fn(), | ||
}; | ||
}); | ||
|
||
afterAll(() => { | ||
window.console = originalConsole; | ||
}); | ||
|
||
describe("getBaseUri", () => { | ||
describe("when config exists", () => { | ||
it("returns correct URI", () => { | ||
expect(getBaseUri()).toBe("http://www.example.com"); | ||
}); | ||
|
||
it("does not print an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe("when config does not exist", () => { | ||
beforeEach(() => { | ||
delete window.IKSDK; | ||
getBaseUri(); | ||
}); | ||
|
||
it("prints an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
"[IKUISDK] Did not find baseUri in config. Library has not been initialized probably.", | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getApplicationId", () => { | ||
describe("when config exists", () => { | ||
it("returns correct application ID", () => { | ||
expect(getApplicationId()).toBe("12345"); | ||
}); | ||
|
||
it("does not print an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe("when config does not exist", () => { | ||
beforeEach(() => { | ||
delete window.IKSDK; | ||
getApplicationId(); | ||
}); | ||
|
||
it("prints an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
"[IKUISDK] Did not find applicationId in config. Library has not been initialized probably.", | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getTenantId", () => { | ||
describe("when config exists", () => { | ||
it("returns correct tenant ID", () => { | ||
expect(getTenantId()).toBe("tenant-id"); | ||
}); | ||
|
||
it("does not print an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe("when config does not exist", () => { | ||
beforeEach(() => { | ||
delete window.IKSDK; | ||
getTenantId(); | ||
}); | ||
|
||
it("prints an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
"[IKUISDK] Did not find tenantId in config. Library has not been initialized probably.", | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getLocaleConfig", () => { | ||
describe("when config exists", () => { | ||
it("returns correct locale", () => { | ||
expect(getLocaleConfig()).toEqual({ | ||
locale: "sk-SK", | ||
messages: {}, | ||
}); | ||
}); | ||
|
||
it("does not print an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe("when locale config does not exist", () => { | ||
beforeEach(() => { | ||
delete window.IKSDK.config.localeConfig; | ||
}); | ||
|
||
it("returns default locale", () => { | ||
expect(getLocaleConfig()).toEqual(enUSLocale); | ||
}); | ||
|
||
it("does not print an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(0); | ||
}); | ||
}); | ||
|
||
describe("when config does not exist", () => { | ||
beforeEach(() => { | ||
delete window.IKSDK; | ||
getLocaleConfig(); | ||
}); | ||
|
||
it("prints an error to the console", () => { | ||
expect(console.error).toBeCalledTimes(1); | ||
expect(console.error.mock.calls[0]).toEqual([ | ||
"[IKUISDK] Did not find config. Library has not been initialized probably.", | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getBaseAuthUrl", () => { | ||
it("returns correct auth URL", () => { | ||
expect(getBaseAuthUrl()).toBe("http://www.example.com/auth/12345"); | ||
}); | ||
}); |
106 changes: 106 additions & 0 deletions
106
lib/services/core/lib/__tests__/locale-provider.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const { getLocaleConfig } = require("../config"); | ||
// const { enUSLocale } = require("../../locale/en-US"); | ||
const { getLocalizedMessage } = require("../locale-provider"); | ||
const IntlMessageFormat = require("intl-messageformat").default; | ||
|
||
jest.mock("../config", () => ({ | ||
getLocaleConfig: jest.fn(), | ||
})); | ||
|
||
jest.mock("intl-messageformat", () => { | ||
const formatMock = jest.fn().mockImplementation(() => `Formatted: ${this.valueMock}`); | ||
|
||
return { | ||
default: jest.fn().mockImplementation((msg) => { | ||
this.valueMock = msg; | ||
return { | ||
format: formatMock, | ||
}; | ||
}), | ||
formatMock, | ||
}; | ||
}); | ||
|
||
jest.mock("../../locale/en-US", () => ({ | ||
enUSLocale: { | ||
locale: "en-US", | ||
messages: { | ||
key1: "Default message #1", | ||
key2: "Default message #2", | ||
}, | ||
}, | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("findMessageByKey", () => { | ||
const values = { value: "xxx" }; | ||
let formatMock; | ||
let returnedValue; | ||
|
||
beforeAll(() => { | ||
formatMock = require("intl-messageformat").formatMock; | ||
}); | ||
|
||
describe("when locale config contains a key", () => { | ||
beforeEach(() => { | ||
getLocaleConfig.mockImplementation(() => ({ | ||
locale: "custom-CUSTOM", | ||
messages: { | ||
key1: "Localized message #1", | ||
}, | ||
})); | ||
returnedValue = getLocalizedMessage("key1", values); | ||
}); | ||
|
||
it("formats message", () => { | ||
expect(IntlMessageFormat).toBeCalledTimes(1); | ||
expect(IntlMessageFormat.mock.calls[0]).toEqual(["Localized message #1", "custom-CUSTOM"]); | ||
expect(formatMock).toBeCalledTimes(1); | ||
expect(formatMock.mock.calls[0]).toEqual([values]); | ||
expect(returnedValue).toBe("Formatted: Localized message #1"); | ||
}); | ||
}); | ||
|
||
describe("when locale config does not contain a key", () => { | ||
beforeEach(() => { | ||
getLocaleConfig.mockImplementation(() => ({ | ||
locale: "custom-CUSTOM", | ||
messages: { | ||
key1: "Localized message #1", | ||
}, | ||
})); | ||
returnedValue = getLocalizedMessage("key2", values); | ||
}); | ||
|
||
it("formats message", () => { | ||
expect(IntlMessageFormat).toBeCalledTimes(1); | ||
expect(IntlMessageFormat.mock.calls[0]).toEqual(["Default message #2", "custom-CUSTOM"]); | ||
expect(formatMock).toBeCalledTimes(1); | ||
expect(formatMock.mock.calls[0]).toEqual([values]); | ||
expect(returnedValue).toBe("Formatted: Default message #2"); | ||
}); | ||
}); | ||
|
||
describe("when neither locale config nor default config does not contain a key", () => { | ||
beforeEach(() => { | ||
getLocaleConfig.mockImplementation(() => ({ | ||
locale: "custom-CUSTOM", | ||
messages: { | ||
key1: "Localized message #1", | ||
}, | ||
})); | ||
returnedValue = getLocalizedMessage("key3"); | ||
}); | ||
|
||
it("formats message", () => { | ||
expect(IntlMessageFormat).toBeCalledTimes(1); | ||
expect(IntlMessageFormat.mock.calls[0]).toEqual(["", "custom-CUSTOM"]); | ||
expect(formatMock).toBeCalledTimes(1); | ||
expect(formatMock.mock.calls[0]).toEqual([{}]); | ||
expect(returnedValue).toBe("Formatted: "); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.