Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/core/src/__test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,35 @@ describe('initial options', () => {

expect(tolgee.getInitialOptions().apiUrl).toEqual('http://localhost:8000');
});

it('overrideCredentials with branch', () => {
const tolgee = TolgeeCore().init({
language: 'en',
apiUrl: 'http://localhost:8080',
});

tolgee.overrideCredentials({
apiUrl: 'http://localhost:8000',
apiKey: 'test',
branch: 'feature-x',
});

expect(tolgee.getInitialOptions().branch).toEqual('feature-x');
});

it('overrideCredentials branch overrides init branch', () => {
const tolgee = TolgeeCore().init({
language: 'en',
apiUrl: 'http://localhost:8080',
branch: 'original',
});

tolgee.overrideCredentials({
apiUrl: 'http://localhost:8000',
apiKey: 'test',
branch: 'override',
});

expect(tolgee.getInitialOptions().branch).toEqual('override');
});
});
1 change: 1 addition & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export type DevCredentials =
apiUrl?: string;
apiKey?: string;
projectId?: string | number;
branch?: string;
};
export type WrapperMiddleware = {
unwrap: WrapperUnwrapFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { loadInContextLib } from './loadInContextLib';

export const API_KEY_LOCAL_STORAGE = '__tolgee_apiKey';
export const API_URL_LOCAL_STORAGE = '__tolgee_apiUrl';
export const BRANCH_LOCAL_STORAGE = '__tolgee_branch';

function getCredentials() {
const apiKey = sessionStorage.getItem(API_KEY_LOCAL_STORAGE) || undefined;
const apiUrl = sessionStorage.getItem(API_URL_LOCAL_STORAGE) || undefined;
const branch = sessionStorage.getItem(BRANCH_LOCAL_STORAGE) || undefined;

if (!apiKey || !apiUrl) {
return undefined;
Expand All @@ -16,12 +18,14 @@ function getCredentials() {
return {
apiKey,
apiUrl,
...(branch !== undefined ? { branch } : {}),
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

function clearSessionStorage() {
sessionStorage.removeItem(API_KEY_LOCAL_STORAGE);
sessionStorage.removeItem(API_URL_LOCAL_STORAGE);
sessionStorage.removeItem(BRANCH_LOCAL_STORAGE);
}

function onDocumentReady(callback: () => void) {
Expand Down Expand Up @@ -70,6 +74,7 @@ if (sessionStorageAvailable()) {
config: {
apiUrl: tolgee.getInitialOptions().apiUrl || '',
apiKey: tolgee.getInitialOptions().apiKey || '',
branch: tolgee.getInitialOptions().branch,
},
}) as const;

Expand Down
45 changes: 44 additions & 1 deletion packages/web/src/package/__test__/browser.extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const handshakerUpdate = jest.fn(() => Promise.resolve());
const Handshaker = jest.fn(() => ({ update: handshakerUpdate }));
const loadInContextLib = jest.fn(() => Promise.resolve(() => {}));
const inContextToolsFactory = jest.fn(() => (tolgee: any) => tolgee);
const loadInContextLib = jest.fn(() => Promise.resolve(inContextToolsFactory));

jest.mock('../tools/extension', () => ({
Handshaker,
Expand All @@ -21,13 +22,15 @@ import { BrowserExtensionPlugin } from '../typedIndex';
import {
API_KEY_LOCAL_STORAGE,
API_URL_LOCAL_STORAGE,
BRANCH_LOCAL_STORAGE,
} from '../BrowserExtensionPlugin/BrowserExtensionPlugin';
import { readFile } from 'fs/promises';
import { join } from 'path';

describe('compatibility with browser extension', () => {
afterEach(() => {
sessionStorage.clear();
jest.clearAllMocks();
});

it('sends correct data to extension', async () => {
Expand All @@ -39,13 +42,32 @@ describe('compatibility with browser extension', () => {
config: {
apiKey: '',
apiUrl: 'test',
branch: undefined,
},
mode: 'production',
uiPresent: true,
uiVersion: undefined,
});
});

it('sends branch from SDK config to extension', async () => {
const tolgee = TolgeeCore().init({
language: 'en',
apiUrl: 'test',
branch: 'my-branch',
});
tolgee.addPlugin(BrowserExtensionPlugin());
await tolgee.run();
expect(handshakerUpdate).toBeCalledTimes(1);
expect(handshakerUpdate).toBeCalledWith(
expect.objectContaining({
config: expect.objectContaining({
branch: 'my-branch',
}),
})
);
});

it('loads in-context lib if session storage is set', async () => {
sessionStorage.setItem(API_KEY_LOCAL_STORAGE, 'test');
sessionStorage.setItem(API_URL_LOCAL_STORAGE, 'test');
Expand All @@ -57,6 +79,27 @@ describe('compatibility with browser extension', () => {
expect(loadInContextLib).toBeCalledTimes(1);
});

it('picks up branch from sessionStorage', async () => {
sessionStorage.setItem(API_KEY_LOCAL_STORAGE, 'test');
sessionStorage.setItem(API_URL_LOCAL_STORAGE, 'test');
sessionStorage.setItem(BRANCH_LOCAL_STORAGE, 'my-branch');

const tolgee = TolgeeCore().init({ language: 'en' });
tolgee.addPlugin(BrowserExtensionPlugin());
await tolgee.run();

expect(loadInContextLib).toBeCalledTimes(1);
expect(inContextToolsFactory).toBeCalledWith(
expect.objectContaining({
credentials: {
apiKey: 'test',
apiUrl: 'test',
branch: 'my-branch',
},
})
);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('builded module is valid', async () => {
// this test works only after build
const fileContent = await readFile(
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/package/tools/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type LibConfig = {
config: {
apiUrl: string;
apiKey: string;
branch?: string;
};
};

Expand Down
Loading