import {postToParent} from '../crossDomainMessaging'

describe('cross domain messaging', () => {
  describe('#postToParent', () => {
    it('does not error without a parent', () => {
      const messageData = {}
      const fakeWindowWithoutParent = {}
      expect(
        () => { postToParent(messageData, fakeWindowWithoutParent) }
      ).to.not.throw(Error)
    })

    it('calls postMessage on the parent with the data provided', () => {
      const postMessageStub = sinon.stub()
      const fakeWindowWithParent = {
        parent: {
          postMessage: postMessageStub
        }
      }
      const messageData = {subject: 'quizzesNext.testMe'}
      postToParent(messageData, fakeWindowWithParent)
      expect(postMessageStub.calledWith(messageData)).to.be.true()
      postMessageStub.resetHistory()
    })
  })
})
