|
| 1 | +import Vue, { VNode } from 'vue'; |
| 2 | +import { |
| 3 | + loadById, |
| 4 | + mountComponent, |
| 5 | + renderInPlaceholder, |
| 6 | + renderVue, |
| 7 | +} from '..'; |
| 8 | + |
| 9 | +describe('loadById', () => { |
| 10 | + beforeEach(() => { |
| 11 | + document.body.innerHTML = ''; |
| 12 | + }); |
| 13 | + |
| 14 | + test('should load payload by id', () => { |
| 15 | + document.body.innerHTML = ` |
| 16 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"></div> |
| 17 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><!--{"title":"Ara Framework"}--></script> |
| 18 | + `; |
| 19 | + |
| 20 | + const payload = loadById('Example', 'd0a0b082-dad0-4bf2-ae4f-08eff16575b4'); |
| 21 | + |
| 22 | + const { node, data } = payload; |
| 23 | + |
| 24 | + expect(node.getAttribute('data-hypernova-key')).toEqual('Example'); |
| 25 | + expect(node.getAttribute('data-hypernova-id')).toEqual('d0a0b082-dad0-4bf2-ae4f-08eff16575b4'); |
| 26 | + expect(data).toEqual({ |
| 27 | + title: 'Ara Framework', |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + test('should not load payload by id', () => { |
| 32 | + const payload = loadById('Example', 'd0a0b082-dad0-4bf2-ae4f-08eff16575b4'); |
| 33 | + |
| 34 | + expect(payload).toBeNull(); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('mountComponent', () => { |
| 39 | + beforeEach(() => { |
| 40 | + document.body.innerHTML = ''; |
| 41 | + }); |
| 42 | + |
| 43 | + test('should mount component correctly', () => { |
| 44 | + document.body.innerHTML = '<div id="app"><div>'; |
| 45 | + |
| 46 | + const app = Vue.extend({ |
| 47 | + props: ['title'], |
| 48 | + render(h): VNode { |
| 49 | + return h('h1', {}, this.title); |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const node = document.getElementById('app'); |
| 54 | + |
| 55 | + mountComponent(app, node, { title: 'Ara Framework' }); |
| 56 | + |
| 57 | + expect(node.innerHTML).toEqual('<h1>Ara Framework</h1>'); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('renderInPlaceholder', () => { |
| 62 | + beforeEach(() => { |
| 63 | + document.body.innerHTML = ''; |
| 64 | + }); |
| 65 | + |
| 66 | + test('should render component in placeholder correctly', () => { |
| 67 | + document.body.innerHTML = ` |
| 68 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"></div> |
| 69 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><!--{"title":"Ara Framework"}--></script> |
| 70 | + `; |
| 71 | + |
| 72 | + const app = Vue.extend({ |
| 73 | + props: ['title'], |
| 74 | + render(h): VNode { |
| 75 | + return h('h1', {}, this.title); |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + renderInPlaceholder('Example', app, 'd0a0b082-dad0-4bf2-ae4f-08eff16575b4'); |
| 80 | + |
| 81 | + const expectedHTML = ` |
| 82 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><h1>Ara Framework</h1></div> |
| 83 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><!--{"title":"Ara Framework"}--></script> |
| 84 | + `; |
| 85 | + expect(document.body.innerHTML).toEqual(expectedHTML); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('renderVue', () => { |
| 90 | + beforeEach(() => { |
| 91 | + document.body.innerHTML = ''; |
| 92 | + }); |
| 93 | + |
| 94 | + test('should render all the components in the body', () => { |
| 95 | + document.body.innerHTML = ` |
| 96 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"></div> |
| 97 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><!--{"title":"Ara Framework"}--></script> |
| 98 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b5"></div> |
| 99 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b5"><!--{"title":"Ara Framework 2"}--></script> |
| 100 | + `; |
| 101 | + |
| 102 | + const app = Vue.extend({ |
| 103 | + props: ['title'], |
| 104 | + render(h): VNode { |
| 105 | + return h('h1', {}, this.title); |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + renderVue('Example', app); |
| 110 | + |
| 111 | + const expectedHTML = ` |
| 112 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><h1>Ara Framework</h1></div> |
| 113 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b4"><!--{"title":"Ara Framework"}--></script> |
| 114 | + <div data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b5"><h1>Ara Framework 2</h1></div> |
| 115 | + <script type="application/json" data-hypernova-key="Example" data-hypernova-id="d0a0b082-dad0-4bf2-ae4f-08eff16575b5"><!--{"title":"Ara Framework 2"}--></script> |
| 116 | + `; |
| 117 | + |
| 118 | + expect(document.body.innerHTML).toEqual(expectedHTML); |
| 119 | + }); |
| 120 | +}); |
0 commit comments