1 | 'use strict'
|
2 |
|
3 | const assert = require( 'assert' )
|
4 | const domUtils = require( '@mojule/dom-utils' )
|
5 | const is = require( '@mojule/is' )
|
6 | const Mmon = require( '@mojule/mmon' )
|
7 | const Tree = require( '@mojule/tree' )
|
8 | const jsdom = require( 'jsdom' )
|
9 | const Components = require( '../src' )
|
10 |
|
11 | const { stringify } = domUtils
|
12 | const { ReadComponents } = Components
|
13 |
|
14 | const { JSDOM } = jsdom
|
15 | const dom = new JSDOM( '<!doctype>' )
|
16 | const { document } = dom.window
|
17 |
|
18 | const options = { document }
|
19 |
|
20 | const readComponents = ReadComponents( options )
|
21 |
|
22 | describe( 'Components', () => {
|
23 | let componentApi
|
24 |
|
25 | it( 'gets components', done => {
|
26 | readComponents( './test/fixtures/components', ( err, components ) => {
|
27 | if( err ) done( err )
|
28 |
|
29 | assert( is.object( components ) )
|
30 | assert( Object.keys( components ).length > 0 )
|
31 |
|
32 | componentApi = Components( components, document )
|
33 |
|
34 | done()
|
35 | })
|
36 | })
|
37 |
|
38 | it( 'gets dom', () => {
|
39 | const mmon = `
|
40 | document>
|
41 | title: Home
|
42 | header>
|
43 | links[]
|
44 | {}
|
45 | title: Home
|
46 | uri: /
|
47 | {}
|
48 | title: "Contact Us"
|
49 | uri: /contact-us
|
50 | main>
|
51 | footer>
|
52 | `
|
53 |
|
54 | const modelTree = Tree.deserialize( Mmon.parse( mmon ) )
|
55 | const rendered = componentApi.render( modelTree )
|
56 | const html = stringify( rendered.node )
|
57 |
|
58 | assert( is.string( html ) && html.length > 0 )
|
59 | })
|
60 |
|
61 | it( 'getData miss', () => {
|
62 | assert.equal( componentApi.getTemplate( 'nope' ), undefined )
|
63 | })
|
64 |
|
65 | it( 'bad component path', done => {
|
66 | readComponents( 'nope', err => {
|
67 | assert( err )
|
68 | done()
|
69 | })
|
70 | })
|
71 |
|
72 | it( 'from model tree', () => {
|
73 | const mmon = `
|
74 | document>
|
75 | styles[]
|
76 | {}
|
77 | text: body{}
|
78 | {}
|
79 | uri: /css
|
80 | main>
|
81 | `
|
82 |
|
83 | const parsed = Mmon.parse( mmon )
|
84 | const modelTree = Tree.deserialize( parsed )
|
85 | const rendered = componentApi.render( modelTree )
|
86 | const html = stringify( rendered.node )
|
87 |
|
88 | assert( is.string( html ) && html.length > 0 )
|
89 | })
|
90 | })
|