@shopware-ag/admin-extension-sdk
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -49,3 +49,7 @@ import { ShopwareMessageTypes } from './messages.types'; | ||
*/ | ||
export declare function handle<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, method: (data: MessageDataType<MESSAGE_TYPE>) => Promise<ShopwareMessageTypes[MESSAGE_TYPE]['responseType']> | ShopwareMessageTypes[MESSAGE_TYPE]['responseType']): () => void; | ||
export declare function handle<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, method: (data: MessageDataType<MESSAGE_TYPE>, additionalInformation: { | ||
_event_: MessageEvent<string>; | ||
}) => Promise<ShopwareMessageTypes[MESSAGE_TYPE]['responseType']> | ShopwareMessageTypes[MESSAGE_TYPE]['responseType']): () => void; | ||
export declare function publish<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, data: ShopwareMessageTypes[MESSAGE_TYPE]['responseType']): Promise<(void | Awaited<ShopwareMessageTypes[MESSAGE_TYPE]["responseType"]> | null)[]>; | ||
export declare function subscribe<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, method: (data: ShopwareMessageTypes[MESSAGE_TYPE]['responseType']) => void | Promise<unknown>): () => void; | ||
/** | ||
@@ -61,1 +65,2 @@ * Function overloading for these two cases: | ||
export declare function createHandler<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(messageType: MESSAGE_TYPE): (method: (data: MessageDataType<MESSAGE_TYPE>) => Promise<ShopwareMessageTypes[MESSAGE_TYPE]['responseType']> | ShopwareMessageTypes[MESSAGE_TYPE]['responseType']) => () => void; | ||
export declare function createSubscriber<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(messageType: MESSAGE_TYPE): (method: (data: ShopwareMessageTypes[MESSAGE_TYPE]['responseType']) => void | Promise<unknown>) => () => void; |
@@ -14,2 +14,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
* ---------------- | ||
* DATA STORES FOR REGISTRIES | ||
* ---------------- | ||
*/ | ||
const sourceRegistry = new Set(); | ||
/** | ||
* ---------------- | ||
* MAIN FUNCTIONS FOR EXPORT | ||
@@ -40,3 +46,6 @@ * ---------------- | ||
const message = JSON.stringify(messageData); | ||
return new Promise((resolve) => { | ||
// set value if send was resolved | ||
let isResolved = false; | ||
const timeoutMs = 3000; | ||
return new Promise((resolve, reject) => { | ||
const callbackHandler = function (event) { | ||
@@ -69,9 +78,29 @@ if (typeof event.data !== 'string') { | ||
window.removeEventListener('message', callbackHandler); | ||
// return the data | ||
resolve(shopwareResponseData._response); | ||
// only return the data if the request is not timed out | ||
if (!isResolved) { | ||
isResolved = true; | ||
// return the data | ||
resolve(shopwareResponseData._response); | ||
} | ||
}; | ||
window.addEventListener('message', callbackHandler); | ||
targetWindow | ||
? targetWindow.postMessage(message, window.parent.origin) | ||
: window.parent.postMessage(message, window.parent.origin); | ||
// @ts-expect-error - Cypress tests run inside iframe. Therefore same level communication is not possible | ||
if (window.parent.__Cypress__) { | ||
targetWindow | ||
? targetWindow.postMessage(message, window.parent.origin) | ||
: window.postMessage(message, window.parent.origin); | ||
} | ||
else { | ||
targetWindow | ||
? targetWindow.postMessage(message, window.parent.origin) | ||
: window.parent.postMessage(message, window.parent.origin); | ||
} | ||
// send timeout when noone sends data back or handler freezes | ||
setTimeout(() => { | ||
// only runs when is not resolved | ||
if (isResolved) { | ||
return; | ||
} | ||
reject('Send timeout expired. It could be possible that no handler for the postMessage request exists or that the handler freezed.'); | ||
}, timeoutMs); | ||
}); | ||
@@ -111,3 +140,3 @@ } | ||
deserializeMessageData(shopwareMessageData); | ||
const responseValue = yield Promise.resolve(method(shopwareMessageData._data)); | ||
const responseValue = yield Promise.resolve(method(shopwareMessageData._data, { _event_: event })); | ||
const responseMessage = { | ||
@@ -138,2 +167,13 @@ _callbackId: shopwareMessageData._callbackId, | ||
} | ||
export function publish(type, data) { | ||
const sendPromises = [...sourceRegistry].map((source) => { | ||
// Disable error handling because not every window need to react to the data | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
return send(type, data, source).catch(() => { }); | ||
}); | ||
return Promise.all(sendPromises); | ||
} | ||
export function subscribe(type, method) { | ||
return handle(type, method); | ||
} | ||
export function createSender(messageType, baseMessageOptions) { | ||
@@ -145,4 +185,25 @@ return (messageOptions) => send(messageType, Object.assign(Object.assign({}, baseMessageOptions), messageOptions)); | ||
} | ||
export function createSubscriber(messageType) { | ||
return (method) => subscribe(messageType, method); | ||
} | ||
/** | ||
* ---------------- | ||
* IIFE for default handler | ||
* ---------------- | ||
*/ | ||
(() => __awaiter(void 0, void 0, void 0, function* () { | ||
// handle registrations at current window | ||
handle('__registerWindow__', (_, additionalOptions) => { | ||
if (additionalOptions._event_.source) { | ||
sourceRegistry.add(additionalOptions._event_.source); | ||
} | ||
else { | ||
sourceRegistry.add(window); | ||
} | ||
}); | ||
// register at parent window | ||
yield send('__registerWindow__', {}); | ||
}))().catch((e) => console.error(e)); | ||
/** | ||
* ---------------- | ||
* INTERNAL HELPER FUNCTIONS | ||
@@ -149,0 +210,0 @@ * ---------------- |
@@ -10,3 +10,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { send, handle, createSender, createHandler } from './channel'; | ||
import { send, handle, createSender, createHandler, subscribe, publish } from './channel'; | ||
describe('Test the channel bridge from iFrame to admin', () => { | ||
@@ -31,3 +31,3 @@ it('should send "reload" command to the admin', (done) => { | ||
}); | ||
it('should get value back from admin', (done) => { | ||
it('should get value back from admin', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const PAGE_TITLE = 'Awesome page title'; | ||
@@ -37,9 +37,7 @@ const removeListener = handle('getPageTitle', () => { | ||
}); | ||
send('getPageTitle', {}).then((pageTitle) => { | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
done(); | ||
}); | ||
}); | ||
it('should create a sender and handler with required options', (done) => { | ||
const pageTitle = yield send('getPageTitle', {}); | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
})); | ||
it('should create a sender and handler with required options', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const getPageTitle = createSender('getPageTitle'); | ||
@@ -51,17 +49,13 @@ const handlePageTitle = createHandler('getPageTitle'); | ||
}); | ||
getPageTitle({}).then((pageTitle) => { | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
done(); | ||
}); | ||
}); | ||
it('should create a sender and handler with optional options', (done) => { | ||
const pageTitle = yield getPageTitle({}); | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
})); | ||
it('should create a sender and handler with optional options', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const reload = createSender('windowReload', {}); | ||
const handleReload = createHandler('windowReload'); | ||
const removeListener = handleReload(() => { }); | ||
reload().then(() => { | ||
removeListener(); | ||
done(); | ||
}); | ||
}); | ||
yield reload(); | ||
removeListener(); | ||
})); | ||
it('should convert functions in options and call them on the handler side', (done) => { | ||
@@ -99,3 +93,3 @@ const buttonMethodMock = jest.fn(() => { }); | ||
}); | ||
it('should convert functions in options and call them on the handler side with arguments and return value', (done) => { | ||
it('should convert functions in options and call them on the handler side with arguments and return value', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const methodMock = jest.fn((firstNumber, secondNumber) => { | ||
@@ -109,10 +103,40 @@ return firstNumber * secondNumber; | ||
}); | ||
sendMultiply({ firstNumber: 7, secondNumber: 8 }) | ||
.then((result) => { | ||
expect(result).toEqual(56); | ||
removeListener(); | ||
done(); | ||
const result = yield sendMultiply({ firstNumber: 7, secondNumber: 8 }); | ||
expect(result).toEqual(56); | ||
removeListener(); | ||
})); | ||
it('should get data from published messages when subscribed', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const localeMethodMock = jest.fn(); | ||
const fallbackLocaleMethodMock = jest.fn(); | ||
const removeSubscription = subscribe('contextLocale', ({ locale, fallbackLocale }) => { | ||
localeMethodMock(locale); | ||
fallbackLocaleMethodMock(fallbackLocale); | ||
}); | ||
}); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(0); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(0); | ||
yield publish('contextLocale', { | ||
locale: 'en-GB', | ||
fallbackLocale: 'en-GB', | ||
}); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(1); | ||
expect(localeMethodMock).toHaveBeenLastCalledWith('en-GB'); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(1); | ||
expect(fallbackLocaleMethodMock).toHaveBeenLastCalledWith('en-GB'); | ||
yield publish('contextLocale', { | ||
locale: 'de-DE', | ||
fallbackLocale: 'en-GB', | ||
}); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(2); | ||
expect(localeMethodMock).toHaveBeenLastCalledWith('de-DE'); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(2); | ||
expect(fallbackLocaleMethodMock).toHaveBeenLastCalledWith('en-GB'); | ||
removeSubscription(); | ||
yield publish('contextLocale', { | ||
locale: 'nl-NL', | ||
fallbackLocale: 'en-GB', | ||
}).catch(() => { }); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(2); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(2); | ||
})); | ||
}); | ||
//# sourceMappingURL=channel.spec.js.map |
@@ -5,2 +5,6 @@ export declare const getLanguage: (messageOptions?: import("../channel").MessageDataType<"contextLanguage"> | undefined) => Promise<{ | ||
}>; | ||
export declare const subscribeLanguage: (method: (data: { | ||
systemLanguageId: string; | ||
languageId: string; | ||
}) => void | Promise<unknown>) => () => void; | ||
export declare const getEnvironment: (messageOptions?: import("../channel").MessageDataType<"contextEnvironment"> | undefined) => Promise<"development" | "production" | "testing">; | ||
@@ -11,2 +15,6 @@ export declare const getLocale: (messageOptions?: import("../channel").MessageDataType<"contextLocale"> | undefined) => Promise<{ | ||
}>; | ||
export declare const subscribeLocale: (method: (data: { | ||
locale: string; | ||
fallbackLocale: string; | ||
}) => void | Promise<unknown>) => () => void; | ||
export declare const getCurrency: (messageOptions?: import("../channel").MessageDataType<"contextCurrency"> | undefined) => Promise<{ | ||
@@ -13,0 +21,0 @@ systemCurrencyISOCode: string; |
@@ -1,6 +0,8 @@ | ||
import { createSender } from '../channel'; | ||
import { createSender, createSubscriber } from '../channel'; | ||
export const getLanguage = createSender('contextLanguage', {}); | ||
export const subscribeLanguage = createSubscriber('contextLanguage'); | ||
export const getEnvironment = createSender('contextEnvironment', {}); | ||
export const getLocale = createSender('contextLocale', {}); | ||
export const subscribeLocale = createSubscriber('contextLocale'); | ||
export const getCurrency = createSender('contextCurrency', {}); | ||
//# sourceMappingURL=index.js.map |
@@ -18,2 +18,3 @@ import { notificationDispatch } from './notification/index'; | ||
__function__: __function__; | ||
__registerWindow__: __registerWindow__; | ||
_multiply: _multiply; | ||
@@ -49,1 +50,4 @@ _subtract: _subtract; | ||
}; | ||
export declare type __registerWindow__ = { | ||
responseType: void; | ||
}; |
export declare const redirect: (messageOptions: import("../channel").MessageDataType<"windowRedirect">) => Promise<void>; | ||
export declare const reload: (messageOptions: import("../channel").MessageDataType<"windowReload">) => Promise<void>; | ||
export declare const reload: (messageOptions?: import("../channel").MessageDataType<"windowReload"> | undefined) => Promise<void>; | ||
/** | ||
@@ -4,0 +4,0 @@ * Redirect to another URL |
import { createSender } from '../channel'; | ||
export const redirect = createSender('windowRedirect'); | ||
export const reload = createSender('windowReload'); | ||
export const reload = createSender('windowReload', {}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@shopware-ag/admin-extension-sdk", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"repository": "git://github.com/shopware/admin-extension-sdk.git", | ||
@@ -39,4 +39,4 @@ "description": "The SDK for App iframes to communicate with the Shopware Adminstration", | ||
"lint:eslint": "eslint ./src --ext .ts", | ||
"e2e:open": "concurrently --handle-input --kill-others --success first \"cypress open\" \"npm run dev:build-watch\" \"npm run dev:serve -- --no-browser\"", | ||
"e2e:run": "concurrently --handle-input --kill-others --success first \"cypress run\" \"npm run dev:build-watch\" \"npm run dev:serve -- --no-browser\"", | ||
"e2e:open": "concurrently --handle-input --kill-others --success first \"cypress open --browser chrome\" \"npm run dev:build-watch\" \"npm run dev:serve -- --no-browser\"", | ||
"e2e:run": "concurrently --handle-input --kill-others --success first \"cypress run --browser chrome\" \"npm run dev:build-watch\" \"npm run dev:serve -- --no-browser\"", | ||
"test": "jest --collectCoverage", | ||
@@ -43,0 +43,0 @@ "test:watch": "jest --watch" |
@@ -49,3 +49,7 @@ import { ShopwareMessageTypes } from './messages.types'; | ||
*/ | ||
export declare function handle<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, method: (data: MessageDataType<MESSAGE_TYPE>) => Promise<ShopwareMessageTypes[MESSAGE_TYPE]['responseType']> | ShopwareMessageTypes[MESSAGE_TYPE]['responseType']): () => void; | ||
export declare function handle<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, method: (data: MessageDataType<MESSAGE_TYPE>, additionalInformation: { | ||
_event_: MessageEvent<string>; | ||
}) => Promise<ShopwareMessageTypes[MESSAGE_TYPE]['responseType']> | ShopwareMessageTypes[MESSAGE_TYPE]['responseType']): () => void; | ||
export declare function publish<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, data: ShopwareMessageTypes[MESSAGE_TYPE]['responseType']): Promise<(void | Awaited<ShopwareMessageTypes[MESSAGE_TYPE]["responseType"]> | null)[]>; | ||
export declare function subscribe<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(type: MESSAGE_TYPE, method: (data: ShopwareMessageTypes[MESSAGE_TYPE]['responseType']) => void | Promise<unknown>): () => void; | ||
/** | ||
@@ -61,1 +65,2 @@ * Function overloading for these two cases: | ||
export declare function createHandler<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(messageType: MESSAGE_TYPE): (method: (data: MessageDataType<MESSAGE_TYPE>) => Promise<ShopwareMessageTypes[MESSAGE_TYPE]['responseType']> | ShopwareMessageTypes[MESSAGE_TYPE]['responseType']) => () => void; | ||
export declare function createSubscriber<MESSAGE_TYPE extends keyof ShopwareMessageTypes>(messageType: MESSAGE_TYPE): (method: (data: ShopwareMessageTypes[MESSAGE_TYPE]['responseType']) => void | Promise<unknown>) => () => void; |
@@ -21,3 +21,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createHandler = exports.createSender = exports.handle = exports.send = void 0; | ||
exports.createSubscriber = exports.createHandler = exports.createSender = exports.subscribe = exports.publish = exports.handle = exports.send = void 0; | ||
const function_serializer_1 = require("./_internals/function-serializer"); | ||
@@ -27,2 +27,8 @@ const utils_1 = require("./_internals/utils"); | ||
* ---------------- | ||
* DATA STORES FOR REGISTRIES | ||
* ---------------- | ||
*/ | ||
const sourceRegistry = new Set(); | ||
/** | ||
* ---------------- | ||
* MAIN FUNCTIONS FOR EXPORT | ||
@@ -53,3 +59,6 @@ * ---------------- | ||
const message = JSON.stringify(messageData); | ||
return new Promise((resolve) => { | ||
// set value if send was resolved | ||
let isResolved = false; | ||
const timeoutMs = 3000; | ||
return new Promise((resolve, reject) => { | ||
const callbackHandler = function (event) { | ||
@@ -82,9 +91,29 @@ if (typeof event.data !== 'string') { | ||
window.removeEventListener('message', callbackHandler); | ||
// return the data | ||
resolve(shopwareResponseData._response); | ||
// only return the data if the request is not timed out | ||
if (!isResolved) { | ||
isResolved = true; | ||
// return the data | ||
resolve(shopwareResponseData._response); | ||
} | ||
}; | ||
window.addEventListener('message', callbackHandler); | ||
targetWindow | ||
? targetWindow.postMessage(message, window.parent.origin) | ||
: window.parent.postMessage(message, window.parent.origin); | ||
// @ts-expect-error - Cypress tests run inside iframe. Therefore same level communication is not possible | ||
if (window.parent.__Cypress__) { | ||
targetWindow | ||
? targetWindow.postMessage(message, window.parent.origin) | ||
: window.postMessage(message, window.parent.origin); | ||
} | ||
else { | ||
targetWindow | ||
? targetWindow.postMessage(message, window.parent.origin) | ||
: window.parent.postMessage(message, window.parent.origin); | ||
} | ||
// send timeout when noone sends data back or handler freezes | ||
setTimeout(() => { | ||
// only runs when is not resolved | ||
if (isResolved) { | ||
return; | ||
} | ||
reject('Send timeout expired. It could be possible that no handler for the postMessage request exists or that the handler freezed.'); | ||
}, timeoutMs); | ||
}); | ||
@@ -125,3 +154,3 @@ } | ||
(0, function_serializer_1.deserializeMessageData)(shopwareMessageData); | ||
const responseValue = yield Promise.resolve(method(shopwareMessageData._data)); | ||
const responseValue = yield Promise.resolve(method(shopwareMessageData._data, { _event_: event })); | ||
const responseMessage = { | ||
@@ -153,2 +182,15 @@ _callbackId: shopwareMessageData._callbackId, | ||
exports.handle = handle; | ||
function publish(type, data) { | ||
const sendPromises = [...sourceRegistry].map((source) => { | ||
// Disable error handling because not every window need to react to the data | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
return send(type, data, source).catch(() => { }); | ||
}); | ||
return Promise.all(sendPromises); | ||
} | ||
exports.publish = publish; | ||
function subscribe(type, method) { | ||
return handle(type, method); | ||
} | ||
exports.subscribe = subscribe; | ||
function createSender(messageType, baseMessageOptions) { | ||
@@ -162,4 +204,26 @@ return (messageOptions) => send(messageType, Object.assign(Object.assign({}, baseMessageOptions), messageOptions)); | ||
exports.createHandler = createHandler; | ||
function createSubscriber(messageType) { | ||
return (method) => subscribe(messageType, method); | ||
} | ||
exports.createSubscriber = createSubscriber; | ||
/** | ||
* ---------------- | ||
* IIFE for default handler | ||
* ---------------- | ||
*/ | ||
(() => __awaiter(void 0, void 0, void 0, function* () { | ||
// handle registrations at current window | ||
handle('__registerWindow__', (_, additionalOptions) => { | ||
if (additionalOptions._event_.source) { | ||
sourceRegistry.add(additionalOptions._event_.source); | ||
} | ||
else { | ||
sourceRegistry.add(window); | ||
} | ||
}); | ||
// register at parent window | ||
yield send('__registerWindow__', {}); | ||
}))().catch((e) => console.error(e)); | ||
/** | ||
* ---------------- | ||
* INTERNAL HELPER FUNCTIONS | ||
@@ -166,0 +230,0 @@ * ---------------- |
@@ -41,3 +41,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}); | ||
it('should get value back from admin', (done) => { | ||
it('should get value back from admin', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const PAGE_TITLE = 'Awesome page title'; | ||
@@ -47,9 +47,7 @@ const removeListener = (0, channel_1.handle)('getPageTitle', () => { | ||
}); | ||
(0, channel_1.send)('getPageTitle', {}).then((pageTitle) => { | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
done(); | ||
}); | ||
}); | ||
it('should create a sender and handler with required options', (done) => { | ||
const pageTitle = yield (0, channel_1.send)('getPageTitle', {}); | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
})); | ||
it('should create a sender and handler with required options', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const getPageTitle = (0, channel_1.createSender)('getPageTitle'); | ||
@@ -61,17 +59,13 @@ const handlePageTitle = (0, channel_1.createHandler)('getPageTitle'); | ||
}); | ||
getPageTitle({}).then((pageTitle) => { | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
done(); | ||
}); | ||
}); | ||
it('should create a sender and handler with optional options', (done) => { | ||
const pageTitle = yield getPageTitle({}); | ||
expect(pageTitle).toEqual(PAGE_TITLE); | ||
removeListener(); | ||
})); | ||
it('should create a sender and handler with optional options', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const reload = (0, channel_1.createSender)('windowReload', {}); | ||
const handleReload = (0, channel_1.createHandler)('windowReload'); | ||
const removeListener = handleReload(() => { }); | ||
reload().then(() => { | ||
removeListener(); | ||
done(); | ||
}); | ||
}); | ||
yield reload(); | ||
removeListener(); | ||
})); | ||
it('should convert functions in options and call them on the handler side', (done) => { | ||
@@ -109,3 +103,3 @@ const buttonMethodMock = jest.fn(() => { }); | ||
}); | ||
it('should convert functions in options and call them on the handler side with arguments and return value', (done) => { | ||
it('should convert functions in options and call them on the handler side with arguments and return value', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const methodMock = jest.fn((firstNumber, secondNumber) => { | ||
@@ -119,11 +113,41 @@ return firstNumber * secondNumber; | ||
}); | ||
sendMultiply({ firstNumber: 7, secondNumber: 8 }) | ||
.then((result) => { | ||
expect(result).toEqual(56); | ||
removeListener(); | ||
done(); | ||
const result = yield sendMultiply({ firstNumber: 7, secondNumber: 8 }); | ||
expect(result).toEqual(56); | ||
removeListener(); | ||
})); | ||
it('should get data from published messages when subscribed', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const localeMethodMock = jest.fn(); | ||
const fallbackLocaleMethodMock = jest.fn(); | ||
const removeSubscription = (0, channel_1.subscribe)('contextLocale', ({ locale, fallbackLocale }) => { | ||
localeMethodMock(locale); | ||
fallbackLocaleMethodMock(fallbackLocale); | ||
}); | ||
}); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(0); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(0); | ||
yield (0, channel_1.publish)('contextLocale', { | ||
locale: 'en-GB', | ||
fallbackLocale: 'en-GB', | ||
}); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(1); | ||
expect(localeMethodMock).toHaveBeenLastCalledWith('en-GB'); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(1); | ||
expect(fallbackLocaleMethodMock).toHaveBeenLastCalledWith('en-GB'); | ||
yield (0, channel_1.publish)('contextLocale', { | ||
locale: 'de-DE', | ||
fallbackLocale: 'en-GB', | ||
}); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(2); | ||
expect(localeMethodMock).toHaveBeenLastCalledWith('de-DE'); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(2); | ||
expect(fallbackLocaleMethodMock).toHaveBeenLastCalledWith('en-GB'); | ||
removeSubscription(); | ||
yield (0, channel_1.publish)('contextLocale', { | ||
locale: 'nl-NL', | ||
fallbackLocale: 'en-GB', | ||
}).catch(() => { }); | ||
expect(localeMethodMock).toHaveBeenCalledTimes(2); | ||
expect(fallbackLocaleMethodMock).toHaveBeenCalledTimes(2); | ||
})); | ||
}); | ||
}); | ||
//# sourceMappingURL=channel.spec.js.map |
@@ -5,2 +5,6 @@ export declare const getLanguage: (messageOptions?: import("../channel").MessageDataType<"contextLanguage"> | undefined) => Promise<{ | ||
}>; | ||
export declare const subscribeLanguage: (method: (data: { | ||
systemLanguageId: string; | ||
languageId: string; | ||
}) => void | Promise<unknown>) => () => void; | ||
export declare const getEnvironment: (messageOptions?: import("../channel").MessageDataType<"contextEnvironment"> | undefined) => Promise<"development" | "production" | "testing">; | ||
@@ -11,2 +15,6 @@ export declare const getLocale: (messageOptions?: import("../channel").MessageDataType<"contextLocale"> | undefined) => Promise<{ | ||
}>; | ||
export declare const subscribeLocale: (method: (data: { | ||
locale: string; | ||
fallbackLocale: string; | ||
}) => void | Promise<unknown>) => () => void; | ||
export declare const getCurrency: (messageOptions?: import("../channel").MessageDataType<"contextCurrency"> | undefined) => Promise<{ | ||
@@ -13,0 +21,0 @@ systemCurrencyISOCode: string; |
@@ -12,9 +12,11 @@ (function (factory) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getCurrency = exports.getLocale = exports.getEnvironment = exports.getLanguage = void 0; | ||
exports.getCurrency = exports.subscribeLocale = exports.getLocale = exports.getEnvironment = exports.subscribeLanguage = exports.getLanguage = void 0; | ||
const channel_1 = require("../channel"); | ||
exports.getLanguage = (0, channel_1.createSender)('contextLanguage', {}); | ||
exports.subscribeLanguage = (0, channel_1.createSubscriber)('contextLanguage'); | ||
exports.getEnvironment = (0, channel_1.createSender)('contextEnvironment', {}); | ||
exports.getLocale = (0, channel_1.createSender)('contextLocale', {}); | ||
exports.subscribeLocale = (0, channel_1.createSubscriber)('contextLocale'); | ||
exports.getCurrency = (0, channel_1.createSender)('contextCurrency', {}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -18,2 +18,3 @@ import { notificationDispatch } from './notification/index'; | ||
__function__: __function__; | ||
__registerWindow__: __registerWindow__; | ||
_multiply: _multiply; | ||
@@ -49,1 +50,4 @@ _subtract: _subtract; | ||
}; | ||
export declare type __registerWindow__ = { | ||
responseType: void; | ||
}; |
export declare const redirect: (messageOptions: import("../channel").MessageDataType<"windowRedirect">) => Promise<void>; | ||
export declare const reload: (messageOptions: import("../channel").MessageDataType<"windowReload">) => Promise<void>; | ||
export declare const reload: (messageOptions?: import("../channel").MessageDataType<"windowReload"> | undefined) => Promise<void>; | ||
/** | ||
@@ -4,0 +4,0 @@ * Redirect to another URL |
@@ -15,4 +15,4 @@ (function (factory) { | ||
exports.redirect = (0, channel_1.createSender)('windowRedirect'); | ||
exports.reload = (0, channel_1.createSender)('windowReload'); | ||
exports.reload = (0, channel_1.createSender)('windowReload', {}); | ||
}); | ||
//# sourceMappingURL=index.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
100908
1545