summaryrefslogtreecommitdiff
path: root/packages/astro/test/astro-dynamic.test.js
blob: 78f35a45d15b24a96311e0c8e4bf8de16ff0e0aa (plain) (blame)
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
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Dynamic components', () => {
  let fixture;

  before(async () => {
    fixture = await loadFixture({ projectRoot: './fixtures/astro-dynamic/' });
    await fixture.build();
  });

  it('Loads packages that only run code in client', async () => {
    const html = await fixture.readFile('/index.html');

    const $ = cheerio.load(html)
    expect($('script').length).to.eq(2)
  });

  it('Loads pages using client:media hydrator', async () => {
    const root = new URL('http://example.com/media/index.html');
    const html = await fixture.readFile('/media/index.html');
    const $ = cheerio.load(html);

    // test 1: static value rendered

    let js = await fixture.readFile(new URL($('script').attr('src'), root).pathname);
    expect(js).to.include(`value:"(max-width: 700px)"`);

    // test 2: dynamic value rendered
    js = await fixture.readFile(new URL($('script').eq(1).attr('src'), root).pathname);
    expect(js).to.include(`value:"(max-width: 600px)"`);
  });

  it.skip('Loads pages using client:only hydrator', async () => {
    const html = await fixture.readFile('/client-only/index.html');
    const $ = cheerio.load(html);

    // test 1: <astro-root> is empty
    expect($('<astro-root>').html()).to.equal('');

    // Grab the svelte import
    const exp = /import\("(.+?)"\)/g;
    let match, svelteRenderer;
    while ((match = exp.exec(result.contents))) {
      if (match[1].includes('renderers/renderer-svelte/client.js')) {
        svelteRenderer = match[1];
      }
    }

    // test 2: Svelte renderer is on the page
    expect(svelteRenderer).to.be.ok;

    // test 3: Can load svelte renderer
    // const result = await fixture.fetch(svelteRenderer);
    // expect(result.status).to.equal(200);
  });
});