Skip to content

Commit f8a7e9b

Browse files
committed
chore: add vanilla dispatchAction addon
1 parent e8cf8a6 commit f8a7e9b

9 files changed

Lines changed: 74 additions & 9 deletions

File tree

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist/**/*
22
coverage/**/*
33
node_modules/**/*
44
package-lock.json
5+
addons/vanilla/dispatch-action.js

addons/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,3 @@ Addon is a piece of code that you can copy and integrate into your workflow.
77
> They are NOT officially supported but can serve as an inspiration.
88
>
99
> They can be changed or removed without any warning and would NOT be considered a "breaking change".
10-
11-
## RxJs
12-
13-
Version 1.x of PostQuecast relied heavily on RxJs. In newer versions this is no longer the case.
14-
15-
Still, we love RxJs and recognize its power. This is why we prepared the RxJs addon.
16-
It consists of classes and operators to help you achieve your goals.

addons/rxjs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# RxJs
2+
3+
Version 1.x of PostQuecast relied heavily on RxJs. In newer versions this is no longer the case.
4+
5+
Still, we love RxJs and recognize its power. This is why we prepared the RxJs addon.
6+
It consists of classes and operators to help you achieve your goals.

addons/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4+
"allowJs": true,
45
"baseUrl": ".",
56
"paths": {
67
"@wikia/post-quecast": ["../src/index.ts"]

addons/vanilla/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Vanilla
2+
3+
Should you need to dispatch an action from simple iframe you may not want to load PostQuecast library to perform this simple action.
4+
Or you may not be able to install the package. In such a case you can use simple `dispatchAction` function defined in here.
5+
Just copy it to you iframe, and you can dispatch without installing anything.
6+
7+
> Note: It is still better to use installed version of PostQuecast. Code within addons directory can change without warning.

addons/vanilla/dispatch-action.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function dispatchAction(action, channelId) {
2+
channelId = channelId || 'default';
3+
action.timestamp = Date.now();
4+
5+
var data = {
6+
action: action,
7+
channelId: channelId,
8+
private: true,
9+
libId: '@wikia/post-quecast'
10+
};
11+
12+
top.postMessage(data, '*');
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Action, Communicator, setupPostQuecast } from '@wikia/post-quecast';
2+
import { transformIntoHostStub } from '../../src/models/host.stub';
3+
import { dispatchAction } from './dispatch-action';
4+
5+
describe('dispatchAction', () => {
6+
const dateMock = jest.spyOn(Date, 'now');
7+
let communicatorDefault: Communicator;
8+
let communicatorOther: Communicator;
9+
10+
beforeEach(() => {
11+
dateMock.mockReturnValue(10);
12+
transformIntoHostStub(window);
13+
setupPostQuecast();
14+
communicatorDefault = new Communicator();
15+
communicatorOther = new Communicator({ channelId: 'other channel' });
16+
});
17+
18+
it('should receive dispatched message', () => {
19+
const resultsDefault: Action = [];
20+
const resultsOther: Action = [];
21+
22+
communicatorDefault.addListener((a) => resultsDefault.push(a));
23+
communicatorOther.addListener((a) => resultsOther.push(a));
24+
25+
dispatchAction({ type: 'action A' });
26+
dispatchAction({ type: 'action B', payload: 'some data' });
27+
dispatchAction({ type: 'action C' }, 'other channel');
28+
29+
expect(resultsDefault).toEqual([
30+
{ type: 'action A', timestamp: 10 },
31+
{ type: 'action B', payload: 'some data', timestamp: 10 },
32+
]);
33+
expect(resultsOther).toEqual([{ type: 'action C', timestamp: 10 }]);
34+
});
35+
});

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
displayName: 'addons',
1212
roots: ['<rootDir>/addons'],
1313
transform: {
14-
'^.+\\.tsx?$': 'ts-jest',
14+
'^.+\\.[t,j]sx?$': 'ts-jest',
1515
},
1616
globals: {
1717
'ts-jest': {
@@ -25,7 +25,7 @@ module.exports = {
2525
],
2626
collectCoverageFrom: [
2727
'src/**/*.ts',
28-
'addons/**/*.ts',
28+
'addons/**/*.[t,j]s',
2929
'!src/index.ts',
3030
'!src/**/*.stub.ts',
3131
'!src/models/host.ts', // there is only interface there, jest is incorrectly interpreting it

src/models/host.stub.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ export type HostStub = {
88
listeners: Callback[];
99
};
1010

11+
export function transformIntoHostStub(input: object): asserts input is HostStub {
12+
const stub = createHostStub();
13+
14+
Object.keys(stub).forEach((key) => {
15+
// @ts-ignore
16+
input[key] = stub[key];
17+
});
18+
}
19+
1120
export function createHostStub(): HostStub {
1221
const result: HostStub = {} as any;
1322
const listeners: Callback[] = [];

0 commit comments

Comments
 (0)