@maizzle/framework 4.0.0-alpha.2 → 4.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,132 +1,132 @@
1
- const test = require('ava')
2
- const Maizzle = require('../src')
3
-
4
- const path = require('path')
5
- const {readFileSync} = require('fs')
6
-
7
- const fixture = file => readFileSync(path.join(__dirname, 'fixtures', `${file}.html`), 'utf8')
8
- const expected = file => readFileSync(path.join(__dirname, 'expected', `${file}.html`), 'utf8')
9
-
10
- const renderString = (string, options = {}) => Maizzle.render(string, options).then(({html}) => html)
11
-
12
- test('compiles HTML string if no options are passed', async t => {
13
- const source = fixture('basic')
14
-
15
- const html = await renderString(source)
16
-
17
- t.is(html, source)
18
- })
19
-
20
- test('uses environment config file(s) if available', async t => {
21
- const source = fixture('useConfig')
22
-
23
- const html = await renderString(source, {maizzle: {env: 'maizzle-ci'}})
24
-
25
- t.is(html, expected('useConfig'))
26
- })
27
-
28
- test('inheritance', async t => {
29
- let html = await renderString(fixture('inheritance'))
30
- html = html.replace(/[^\S\r\n]+$/gm, '').trim()
31
-
32
- t.is(html, expected('inheritance').trim())
33
- })
34
-
35
- test('throws if first argument is not an HTML string', async t => {
36
- await t.throwsAsync(async () => {
37
- await renderString(false)
38
- }, {instanceOf: TypeError, message: 'first argument must be an HTML string, received false'})
39
- })
40
-
41
- test('throws if first argument is an empty string', async t => {
42
- await t.throwsAsync(async () => {
43
- await renderString('')
44
- }, {instanceOf: RangeError, message: 'received empty string'})
45
- })
46
-
47
- test('runs the `beforeRender` event', async t => {
48
- const html = await renderString(`<div>{{ page.foo }}</div>`, {
49
- beforeRender(html, config) {
50
- config.foo = 'bar'
51
-
52
- return html
53
- }
54
- })
55
-
56
- t.is(html, `<div>bar</div>`)
57
- })
58
-
59
- test('runs the `afterRender` event', async t => {
60
- const result = await renderString(`<div>foo</div>`, {
61
- afterRender(html, config) {
62
- config.replaceStrings = {
63
- foo: 'baz'
64
- }
65
-
66
- return html
67
- }
68
- })
69
-
70
- t.is(result, `<div>baz</div>`)
71
- })
72
-
73
- test('runs the `afterTransformers` event', async t => {
74
- const result = await renderString(`<div>foo</div>`, {
75
- maizzle: {
76
- title: 'bar'
77
- },
78
- afterTransformers(html, config) {
79
- return html.replace('foo', config.title)
80
- }
81
- })
82
-
83
- t.is(result, `<div>bar</div>`)
84
- })
85
-
86
- test('multiple locals', async t => {
87
- const result = await renderString(`{{ page.one }}, {{ two }}, {{ three }}`, {
88
- maizzle: {
89
- one: 1,
90
- build: {
91
- posthtml: {
92
- expressions: {
93
- locals: {
94
- two: 2
95
- }
96
- }
97
- }
98
- },
99
- locals: {
100
- three: 3
101
- }
102
- }
103
- })
104
-
105
- t.is(result, `1, 2, 3`)
106
- })
107
-
108
- test('prevents overwriting page object', async t => {
109
- const result = await renderString(`{{ page.one }}, {{ two }}, {{ three }}`, {
110
- maizzle: {
111
- one: 1,
112
- build: {
113
- posthtml: {
114
- expressions: {
115
- locals: {
116
- page: {
117
- two: 2
118
- }
119
- }
120
- }
121
- }
122
- },
123
- locals: {
124
- page: {
125
- three: 3
126
- }
127
- }
128
- }
129
- })
130
-
131
- t.is(result, `1, undefined, undefined`)
132
- })
1
+ const test = require('ava')
2
+ const Maizzle = require('../src')
3
+
4
+ const path = require('path')
5
+ const {readFileSync} = require('fs')
6
+
7
+ const fixture = file => readFileSync(path.join(__dirname, 'fixtures', `${file}.html`), 'utf8')
8
+ const expected = file => readFileSync(path.join(__dirname, 'expected', `${file}.html`), 'utf8')
9
+
10
+ const renderString = (string, options = {}) => Maizzle.render(string, options).then(({html}) => html)
11
+
12
+ test('compiles HTML string if no options are passed', async t => {
13
+ const source = fixture('basic')
14
+
15
+ const html = await renderString(source)
16
+
17
+ t.is(html, source)
18
+ })
19
+
20
+ test('uses environment config file(s) if available', async t => {
21
+ const source = fixture('useConfig')
22
+
23
+ const html = await renderString(source, {maizzle: {env: 'maizzle-ci'}})
24
+
25
+ t.is(html, expected('useConfig'))
26
+ })
27
+
28
+ test('inheritance', async t => {
29
+ let html = await renderString(fixture('inheritance'))
30
+ html = html.replace(/[^\S\r\n]+$/gm, '').trim()
31
+
32
+ t.is(html, expected('inheritance').trim())
33
+ })
34
+
35
+ test('throws if first argument is not an HTML string', async t => {
36
+ await t.throwsAsync(async () => {
37
+ await renderString(false)
38
+ }, {instanceOf: TypeError, message: 'first argument must be an HTML string, received false'})
39
+ })
40
+
41
+ test('throws if first argument is an empty string', async t => {
42
+ await t.throwsAsync(async () => {
43
+ await renderString('')
44
+ }, {instanceOf: RangeError, message: 'received empty string'})
45
+ })
46
+
47
+ test('runs the `beforeRender` event', async t => {
48
+ const html = await renderString(`<div>{{ page.foo }}</div>`, {
49
+ beforeRender(html, config) {
50
+ config.foo = 'bar'
51
+
52
+ return html
53
+ }
54
+ })
55
+
56
+ t.is(html, `<div>bar</div>`)
57
+ })
58
+
59
+ test('runs the `afterRender` event', async t => {
60
+ const result = await renderString(`<div>foo</div>`, {
61
+ afterRender(html, config) {
62
+ config.replaceStrings = {
63
+ foo: 'baz'
64
+ }
65
+
66
+ return html
67
+ }
68
+ })
69
+
70
+ t.is(result, `<div>baz</div>`)
71
+ })
72
+
73
+ test('runs the `afterTransformers` event', async t => {
74
+ const result = await renderString(`<div>foo</div>`, {
75
+ maizzle: {
76
+ title: 'bar'
77
+ },
78
+ afterTransformers(html, config) {
79
+ return html.replace('foo', config.title)
80
+ }
81
+ })
82
+
83
+ t.is(result, `<div>bar</div>`)
84
+ })
85
+
86
+ test('multiple locals', async t => {
87
+ const result = await renderString(`{{ page.one }}, {{ two }}, {{ three }}`, {
88
+ maizzle: {
89
+ one: 1,
90
+ build: {
91
+ posthtml: {
92
+ expressions: {
93
+ locals: {
94
+ two: 2
95
+ }
96
+ }
97
+ }
98
+ },
99
+ locals: {
100
+ three: 3
101
+ }
102
+ }
103
+ })
104
+
105
+ t.is(result, `1, 2, 3`)
106
+ })
107
+
108
+ test('prevents overwriting page object', async t => {
109
+ const result = await renderString(`{{ page.one }}, {{ two }}, {{ three }}`, {
110
+ maizzle: {
111
+ one: 1,
112
+ build: {
113
+ posthtml: {
114
+ expressions: {
115
+ locals: {
116
+ page: {
117
+ two: 2
118
+ }
119
+ }
120
+ }
121
+ }
122
+ },
123
+ locals: {
124
+ page: {
125
+ three: 3
126
+ }
127
+ }
128
+ }
129
+ })
130
+
131
+ t.is(result, `1, undefined, undefined`)
132
+ })