|
1 | 1 | import React from 'react'
|
2 | 2 | import AnvilEmbedFrame from '../../src/index'
|
| 3 | +import { expect } from 'chai' // Assuming you're using Chai for assertions |
3 | 4 |
|
4 | 5 | describe('AnvilEmbedFrame', function () {
|
5 |
| - def('handleEvent', () => sinon.spy()) |
6 |
| - def('anvilURL', 'https://app.useanvil.com') |
| 6 | + let handleEvent |
| 7 | + const anvilURL = 'https://app.useanvil.com' |
| 8 | + const event = new window.Event('message', { bubbles: true }) |
7 | 9 |
|
8 |
| - def('render', () => shallow( |
9 |
| - <AnvilEmbedFrame |
10 |
| - iframeURL="http://localhost" |
11 |
| - onEvent={$.handleEvent} |
12 |
| - anvilURL={$.anvilURL} |
13 |
| - />, |
14 |
| - )) |
| 10 | + beforeEach(() => { |
| 11 | + handleEvent = sinon.spy() |
| 12 | + }) |
15 | 13 |
|
16 |
| - it('renders', async function () { |
17 |
| - const wrapper = $.render |
18 |
| - expect(wrapper).to.exist |
19 |
| - expect(wrapper.find('iframe')).to.exist |
| 14 | + it('renders', function () { |
| 15 | + const wrapper = shallow( |
| 16 | + <AnvilEmbedFrame |
| 17 | + iframeURL="http://localhost" |
| 18 | + onEvent={handleEvent} |
| 19 | + anvilURL={anvilURL} |
| 20 | + /> |
| 21 | + ) |
| 22 | + expect(wrapper.exists()).to.be.true |
| 23 | + expect(wrapper.find('iframe').exists()).to.be.true |
20 | 24 | })
|
21 | 25 |
|
22 |
| - describe('onEvent', function () { |
23 |
| - it('does not call onEvent when non-anvil url passed in', async function () { |
| 26 | + describe('handleEvent', function () { |
| 27 | + it('does not call onEvent when non-anvil url passed in', function () { |
24 | 28 | const origin = 'https://chess.com'
|
25 | 29 | const data = { action: 'forgeComplete' }
|
26 |
| - const wrapper = $.render |
27 |
| - wrapper.instance().handleEvent({ origin, data }) |
28 |
| - expect($.handleEvent).to.not.have.been.called |
| 30 | + mount( |
| 31 | + <AnvilEmbedFrame |
| 32 | + iframeURL="http://localhost" |
| 33 | + onEvent={handleEvent} |
| 34 | + anvilURL={anvilURL} |
| 35 | + /> |
| 36 | + ) |
| 37 | + event.data = data |
| 38 | + event.origin = origin |
| 39 | + window.dispatchEvent(event) |
| 40 | + expect(handleEvent.called).to.be.false |
29 | 41 | })
|
30 | 42 |
|
31 |
| - it('does not call onEvent when non-object data passed in', async function () { |
32 |
| - const origin = $.anvilURL |
| 43 | + it('does not call onEvent when non-object data passed in', function () { |
| 44 | + const origin = anvilURL |
33 | 45 | const data = 'signerComplete'
|
34 |
| - const wrapper = $.render |
35 |
| - wrapper.instance().handleEvent({ origin, data }) |
36 |
| - expect($.handleEvent).to.not.have.been.called |
| 46 | + mount( |
| 47 | + <AnvilEmbedFrame |
| 48 | + iframeURL="http://localhost" |
| 49 | + onEvent={handleEvent} |
| 50 | + anvilURL={anvilURL} |
| 51 | + /> |
| 52 | + ) |
| 53 | + event.data = data |
| 54 | + event.origin = origin |
| 55 | + window.dispatchEvent(event) |
| 56 | + expect(handleEvent.called).to.be.false |
37 | 57 | })
|
38 | 58 |
|
39 |
| - it('calls onEvent successfully', async function () { |
40 |
| - const origin = $.anvilURL |
| 59 | + it('calls onEvent successfully', function () { |
| 60 | + const origin = anvilURL |
41 | 61 | const data = { action: 'castEdit' }
|
42 |
| - const wrapper = $.render |
43 |
| - wrapper.instance().handleEvent({ origin, data }) |
44 |
| - expect($.handleEvent).to.have.been.calledWith(data) |
| 62 | + mount( |
| 63 | + <AnvilEmbedFrame |
| 64 | + iframeURL="http://localhost" |
| 65 | + onEvent={handleEvent} |
| 66 | + anvilURL={anvilURL} |
| 67 | + /> |
| 68 | + ) |
| 69 | + event.data = data |
| 70 | + event.origin = origin |
| 71 | + window.dispatchEvent(event) |
| 72 | + expect(handleEvent.calledWith(data)).to.be.true |
45 | 73 | })
|
46 | 74 | })
|
47 | 75 | })
|
0 commit comments