Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from indykite/IK-1761
Browse files Browse the repository at this point in the history
Unit tests and some fixes
  • Loading branch information
dusan-kovacik-profiq authored Jan 19, 2022
2 parents 91a725c + 40bc2b2 commit 84c16fe
Show file tree
Hide file tree
Showing 52 changed files with 12,522 additions and 2,782 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# misc
.DS_Store
.idea/
.vscode/launch.json
.env.local
.env.development.local
.env.test.local
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog

## 0.1.7

- [FIX] Minor code fixes
- [TEST] Added unit tests

## 0.1.6

- [FIX] Fixed unhandled error thrown when you wanted reset your password with empty email input.
- [FIX] Fixed unhandled error thrown when you wanted reset your password with empty email input.

## 0.1.5

Expand Down
4 changes: 2 additions & 2 deletions lib/services/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const {
loginSetup,
oidcCallback,
} = require("./lib/login");
const oidcSetup = require("./lib/login-fns/oidcSetup");
const singleOidcSetup = require("./lib/login-fns/singleOidcSetup");
const oidcSetup = require("./lib/login/oidcSetup");
const singleOidcSetup = require("./lib/login/singleOidcSetup");
const { registrationFormSetupRequest, register } = require("./lib/register");
const {
forgotPasswordSetupRequest,
Expand Down
159 changes: 159 additions & 0 deletions lib/services/core/lib/__tests__/config.test.js
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 lib/services/core/lib/__tests__/locale-provider.test.js
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: ");
});
});
});
Loading

0 comments on commit 84c16fe

Please sign in to comment.