parent
95c095f366
commit
adcaf2aa48
4 changed files with 60 additions and 22 deletions
@ -0,0 +1,19 @@ |
||||
const receive = (win, callback) => { |
||||
win.addEventListener('message', (e) => { |
||||
let message; |
||||
try { |
||||
message = JSON.parse(e.data); |
||||
} catch (e) { |
||||
// ignore message posted by author of web page
|
||||
return; |
||||
} |
||||
|
||||
callback(message); |
||||
}) |
||||
} |
||||
|
||||
const send = (win, message) => { |
||||
win.postMessage(JSON.stringify(message), '*'); |
||||
} |
||||
|
||||
export { receive, send }; |
@ -0,0 +1,25 @@ |
||||
import { expect } from "chai"; |
||||
import * as messages from '../../src/shared/messages'; |
||||
|
||||
describe('messages', () => { |
||||
describe('#receive', () => { |
||||
it('received a message', (done) => { |
||||
messages.receive(window, (message) => { |
||||
expect(message).to.deep.equal({ type: 'vimvixen.test' }); |
||||
done(); |
||||
}); |
||||
window.postMessage(JSON.stringify({ type: 'vimvixen.test' }), '*'); |
||||
}); |
||||
}); |
||||
|
||||
describe('#send', () => { |
||||
it('sends a message', (done) => { |
||||
window.addEventListener('message', (e) => { |
||||
let json = JSON.parse(e.data); |
||||
expect(json).to.deep.equal({ type: 'vimvixen.test' }); |
||||
done(); |
||||
}); |
||||
messages.send(window, { type: 'vimvixen.test' }); |
||||
}); |
||||
}); |
||||
}); |
Reference in new issue