Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove json assumption from postMessage handler #190

Merged
merged 3 commits into from
Sep 18, 2018
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
4 changes: 2 additions & 2 deletions src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class DefaultMessageBus implements MessageBus {
}

// Make sure topic received matches the one that was subscribed
const { source, "topic": receivedTopic, message } = JSON.parse(event.data);
const { source, "topic": receivedTopic, message } = event.data;

if (source === DefaultMessageBus.messageSource && topic === receivedTopic) {
listener({ topic: topic }, message);
Expand Down Expand Up @@ -173,7 +173,7 @@ export class DefaultMessageBus implements MessageBus {
continue;
}

win.postMessage(JSON.stringify({ source: DefaultMessageBus.messageSource, topic: topic, message: message }), this.container.globalWindow.location.origin);
win.postMessage({ source: DefaultMessageBus.messageSource, topic: topic, message: message }, this.container.globalWindow.location.origin);
}
}

Expand Down
17 changes: 12 additions & 5 deletions tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,15 @@ describe("DefaultMessageBus", () => {
});

it("listener callback attached", (done) => {
bus.subscribe("topic", callback).then((subscriber) => {
subscriber.listener({ origin: "origin", data: JSON.stringify({ source: "desktopJS", topic: "topic", message: "message" }) });
}).then(done);
const handler = (e, data) => {
expect(e.topic).toEqual("topic");
expect(data).toEqual("message");
done();
};

bus.subscribe("topic", handler).then((subscriber) => {
subscriber.listener({ origin: "origin", data: { source: "desktopJS", topic: "topic", message: "message" } });
});
});

it("unsubscribe invokes underlying unsubscribe", (done) => {
Expand All @@ -444,8 +450,9 @@ describe("DefaultMessageBus", () => {
it("publish invokes underling publish", (done) => {
let message: any = { data: "data" };
spyOn(mockWindow, "postMessage").and.callThrough();
bus.publish("topic", message).then(done);
expect(mockWindow.postMessage).toHaveBeenCalledWith(JSON.stringify({ source: "desktopJS", topic: "topic", message: message }), "origin");
bus.publish("topic", message).then(() => {
expect(mockWindow.postMessage).toHaveBeenCalledWith({ source: "desktopJS", topic: "topic", message: message }, "origin");
}).then(done);
});

it("publish with non matching optional name does not invoke underling send", (done) => {
Expand Down