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

Commit

Permalink
NEXT-28243 - improve dataset handling with selector
Browse files Browse the repository at this point in the history
  • Loading branch information
jleifeld committed Jun 20, 2023
1 parent ebbe8ab commit b49813e
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 61 deletions.
48 changes: 42 additions & 6 deletions e2e/channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ test.describe('Privilege tests', () => {

await mainFrame.evaluate(() => {
window.sw_internal.setExtensions({
foo: {
example: {
baseUrl: 'http://localhost:8182',
permissions: {
read: ['product']
Expand Down Expand Up @@ -651,7 +651,7 @@ test.describe('Privilege tests', () => {
}
});

expect(response.errorMessage).toEqual(`Error: Your app is missing the privileges create:product, delete:product, update:product, create:product, delete:product, update:product, create:manufacturer, delete:manufacturer, read:manufacturer, update:manufacturer for action "_collectionTest".`);
expect(response.errorMessage).toEqual(`Error: Your app is missing the privileges create:product, delete:product, update:product, create:manufacturer, delete:manufacturer, read:manufacturer, update:manufacturer for action "_collectionTest".`);
expect(response.isMissingPrivilesErrorInstance).toBe(true);
});

Expand All @@ -660,7 +660,7 @@ test.describe('Privilege tests', () => {

await mainFrame.evaluate(() => {
window.sw_internal.setExtensions({
foo: {
example: {
baseUrl: 'http://localhost:8182',
permissions: {
read: ['product']
Expand Down Expand Up @@ -727,7 +727,7 @@ test.describe('Privilege tests', () => {
// publish dataset
await mainFrame.evaluate(async () => {
window.sw_internal.setExtensions({
foo: {
example: {
baseUrl: 'http://localhost:8182',
permissions: {
read: ['manufacturer']
Expand Down Expand Up @@ -775,7 +775,7 @@ test.describe('Privilege tests', () => {
// publish dataset
await mainFrame.evaluate(async () => {
window.sw_internal.setExtensions({
foo: {
example: {
baseUrl: 'http://localhost:8182',
permissions: {
read: ['product']
Expand Down Expand Up @@ -831,7 +831,7 @@ test.describe('data handling', () => {
expect(data['e2e-test']).toBe('test-string');
});

test('dataset subscriber', async ({ page }) => {
test('dataset subscriber without selector', async ({ page }) => {
const { mainFrame, subFrame } = await setup({ page });

// subscribe to dataset publish
Expand Down Expand Up @@ -860,6 +860,42 @@ test.describe('data handling', () => {
expect(data).toBe('test-string');
});

test('dataset subscriber with selector', async ({ page }) => {
const { mainFrame, subFrame } = await setup({ page });

// subscribe to dataset publish
await subFrame.evaluate(async () => {
return await window.sw.data.subscribe('product_detail', (data) => {
// @ts-expect-error
window.result = { data: data.data };
}, {
selectors: ['name']
});
})

// publish dataset
await mainFrame.evaluate(async () => {
await window.sw.data.register({
id: 'product_detail',
data: {
name: 'T-Shirt',
description: 'An awesome T-Shirt'
},
});
})

// get receiving value
const { data } = await subFrame.evaluate(() => {
// @ts-expect-error
return window.result;
})

// check if receiving value matches
expect(data).toEqual({
name: 'T-Shirt',
});
});

test('dataset update', async ({ page }) => {
const { mainFrame, subFrame } = await setup({ page });

Expand Down
13 changes: 13 additions & 0 deletions e2e/testpage/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ window.sw_internal = {
Entity: EntityClass,
MissingPrivilegesError: MissingPrivilegesError,
}


window.sw_internal.setExtensions({
example: {
baseUrl: 'http://localhost:8182',
permissions: {
create: ['test', 'foo', 'product'],
update: ['test', 'foo', 'product'],
delete: ['test', 'foo', 'product'],
read: ['test', 'foo', 'product'],
}
},
});
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'jsdom',
globals: {
'ts-jest': {
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/_internals/sdkVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* JS file is needed because TypeScript can't use JSON imports
* for UMD builds.
*/
import { version } from '../../package.json';

export default version;
11 changes: 9 additions & 2 deletions src/_internals/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default function validate({
const extension = findExtensionByBaseUrl(origin);

if (!extension) {
console.warn(`No extension found for origin ${origin}`);
return null;
}

Expand All @@ -32,11 +33,17 @@ export default function validate({
if (key === '__type__' && ['__EntityCollection__', '__Entity__'].includes(value as string)) {
const entityName = parentEntry.__entityName__ as string;

if (!entityName) {
return;
}

[...privilegesToCheck].sort().forEach(privilege => {
const permissionsForPrivilege = extension.permissions[privilege];
if (
!permissionsForPrivilege ||
!permissionsForPrivilege.includes(entityName)
(!permissionsForPrivilege ||
!permissionsForPrivilege.includes(entityName))
&&
!privilegeErrors.includes(`${privilege}:${entityName}`)
) {
privilegeErrors.push(`${privilege}:${entityName}`);
}
Expand Down
47 changes: 46 additions & 1 deletion src/channel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
import flushPromises from 'flush-promises';
import { send, handle, createSender, createHandler, subscribe, publish, setExtensions } from './channel';
import type {
send as sendType,
handle as handleType,
createSender as createSenderType,
createHandler as createHandlerType,
subscribe as subscribeType,
publish as publishType,
setExtensions as setExtensionsType,
} from './channel';
import MissingPrivilegesError from './privileges/missing-privileges-error';

// Channel send timout + 1000
jest.setTimeout(8000);

let send: typeof sendType;
let handle: typeof handleType;
let createSender: typeof createSenderType;
let createHandler: typeof createHandlerType;
let subscribe: typeof subscribeType;
let publish: typeof publishType;
let setExtensions: typeof setExtensionsType;

describe('Test the channel bridge from iFrame to admin', () => {
beforeAll(async () => {
window.addEventListener('message', (event: MessageEvent) => {
if (event.origin === '') {
event.stopImmediatePropagation();
const eventWithOrigin: MessageEvent = new MessageEvent('message', {
data: event.data,
origin: window.location.href,
});
window.dispatchEvent(eventWithOrigin);
}
});

const channel = await import('./channel');
send = channel.send;
handle = channel.handle;
createSender = channel.createSender;
createHandler = channel.createHandler;
subscribe = channel.subscribe;
publish = channel.publish;
setExtensions = channel.setExtensions;

setExtensions({
'test-extension': {
baseUrl: 'http://localhost',
permissions: {},
},
});
});

beforeEach(() => {
// reset extensions
setExtensions({});
Expand Down
Loading

0 comments on commit b49813e

Please sign in to comment.