-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
astro-directives.test.js
74 lines (57 loc) · 2.27 KB
/
astro-directives.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Directives', async () => {
let fixture;
before(async () => {
fixture = await loadFixture({ root: './fixtures/astro-directives/' });
await fixture.build();
});
it('Passes define:vars to script elements', async () => {
const html = await fixture.readFile('/define-vars/index.html');
const $ = cheerio.load(html);
expect($('script')).to.have.lengthOf(3);
let i = 0;
for (const script of $('script').toArray()) {
// Wrap script in scope ({}) to avoid redeclaration errors
expect($(script).text().startsWith('(function(){')).to.equal(true);
expect($(script).text().endsWith('})();')).to.equal(true);
if (i < 2) {
// Inline defined variables
expect($(script).toString()).to.include('const foo = "bar"');
} else {
// Convert invalid keys to valid identifiers
expect($(script).toString()).to.include('const dashCase = "bar"');
}
i++;
}
});
it('Passes define:vars to style elements', async () => {
const html = await fixture.readFile('/define-vars/index.html');
const $ = cheerio.load(html);
expect($('style')).to.have.lengthOf(2);
// Inject style attribute on top-level element in page
expect($('html').attr('style').toString()).to.include('--bg: white;');
expect($('html').attr('style').toString()).to.include('--fg: black;');
// Inject style attribute on top-level elements in component
expect($('h1').attr('style').toString()).to.include('--textColor: red;');
});
it('set:html', async () => {
const html = await fixture.readFile('/set-html/index.html');
const $ = cheerio.load(html);
expect($('#text')).to.have.lengthOf(1);
expect($('#text').text()).to.equal('a');
expect($('#zero')).to.have.lengthOf(1);
expect($('#zero').text()).to.equal('0');
expect($('#number')).to.have.lengthOf(1);
expect($('#number').text()).to.equal('1');
expect($('#undefined')).to.have.lengthOf(1);
expect($('#undefined').text()).to.equal('');
expect($('#null')).to.have.lengthOf(1);
expect($('#null').text()).to.equal('');
expect($('#false')).to.have.lengthOf(1);
expect($('#false').text()).to.equal('');
expect($('#true')).to.have.lengthOf(1);
expect($('#true').text()).to.equal('true');
});
});