Skip to content

Commit

Permalink
Merge pull request #219 from schibsted/195/support-locale-option
Browse files Browse the repository at this point in the history
195 Add locale variable to the Simplified Login widget
  • Loading branch information
zamorastp authored Oct 27, 2022
2 parents 0caeb1d + 30a8935 commit 0f50933
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions __tests__/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,31 @@ describe('Identity', () => {
expect(document.getElementsByTagName('body')[0].appendChild).toHaveBeenCalledTimes(1);
expect(window.openSimplifiedLoginWidget).toHaveBeenCalledTimes(1);
});

test('Should pass locale param to simplified login widget', async () => {
const expectedLocale = 'fi';
const stateFn = jest.fn(() => state);
identity._globalSessionService.fetch = jest.fn(() => ({ ok: true, json: () => expectedData }));
identity.login = jest.fn();

document.getElementsByTagName('body')[0].appendChild = jest.fn((el) => {
window.openSimplifiedLoginWidget = jest.fn(async (initialParams, loginHandler) => {
expect(initialParams.locale).toEqual(expectedLocale);
await loginHandler();
expect(identity.login).toHaveBeenCalledWith({
state,
loginHint: expectedData.identifier
});
return true;
});

el.onload();
});

expect(await identity.showSimplifiedLoginWidget({ state: stateFn }, { locale: expectedLocale })).toEqual(true);
expect(document.getElementsByTagName('body')[0].appendChild).toHaveBeenCalledTimes(1);
expect(window.openSimplifiedLoginWidget).toHaveBeenCalledTimes(1);
});
});

describe('logSettings', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/identity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ export type SimplifiedLoginWidgetOptions = {
* - expected encoding of simplified login widget. Could be utf-8 (default), iso-8859-1 or iso-8859-15
*/
encoding: string;
/**
* - expected locale of simplified login widget. Should be provided in a short format like 'nb',
* 'sv'. If not set, a value from the env variable is used.
*/
locale?: "nb"|"sv"|"fi"|"da"|"en";
};
import RESTClient from "./RESTClient";
import SDKError from "./SDKError";
6 changes: 5 additions & 1 deletion src/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ export class Identity extends EventEmitter {
* @return {Promise<boolean|SDKError>} - will resolve to true if widget will be display. Otherwise will throw SDKError
*/
async showSimplifiedLoginWidget(loginParams, options) {
// getUserContextData doens't throw exception
// getUserContextData doesn't throw exception
const userData = await this.getUserContextData();

const queryParams = { client_id: this.clientId };
Expand Down Expand Up @@ -888,6 +888,10 @@ export class Identity extends EventEmitter {
},
};

if (options && options.locale) {
initialParams.locale = options.locale;
}

const loginHandler = async () => {
this.login(Object.assign(await prepareLoginParams(loginParams), {loginHint: userData.identifier}));
};
Expand Down

0 comments on commit 0f50933

Please sign in to comment.