0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00
astro/packages/astro/test/astro-expr.test.js
Nate Moore 17c02925c5
Migrate to new config (#2962)
* wip: config migration

* fix: formatting

* refactor: projectRoot -> root

* refactor: pageUrlFormat -> format

* refactor: buildOptions.site -> site

* refactor: public -> publicDir

* refactor: dist -> outDir

* refactor: styleOptions -> style

* fix: some dist tests -> outDir

* refactor: remove legacyBuild (with TODOs)

* refactor: more legacyBuild cleanup

* refactor: server host and port

* fix: remove experimentalStaticBuild CLI flag

* refactor: src -> srcDir

* refactor: devOptions.trailing -> trailing

* refactor: remove sitemap + related flags

* refactor: experimentalSSR -> experimental.ssr

* fix: last devOptions

* refactor: drafts -> markdown.drafts

* fix: TS error on port as const

* refactor: remove pages

* refactor: more --project-root updates

* refactor: markdownOptions -> markdown

* fix: remaining type errors

* feat: update AstroUserConfig

* refactor: update CLI flag mapper + server mapper

* fix: loadFixture projectRoot

* fix: merge CLI flags before validating / transforming

* wip: attempt to fix bad createRouteManifest config

* refactor: combine config.base and config.site

* fix: skip route manifest test for now

* fix: site and base handling

* refactor: update failing config testes

* fix: build failure

* feat: update config types with migration help

* chore: update types

* fix(deno): update deno fixture

* chore: remove config migration logic

* chore: remove logLevel

* chore: clean-up config types

* chore: update config warning

* chore: add changeset

* Sitemap Integration (#2965)

* feat: add sitemap filter config option

* feat: add canonicalURL sitemap config option

* docs: update sitemap README

* fix: update for new config

* fix: filter not being applied

* chore: changeset

Co-authored-by: bholmesdev <hey@bholmes.dev>

* fred pass

* fix: Astro.resolve typo

* fix: public => publicDir

Co-authored-by: bholmesdev <hey@bholmes.dev>
Co-authored-by: Fred K. Schott <fkschott@gmail.com>
2022-04-02 12:29:59 -06:00

117 lines
3.6 KiB
JavaScript

import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
describe('Expressions', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/astro-expr/',
});
await fixture.build();
});
it('Can load page', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
for (let col of ['red', 'yellow', 'blue']) {
expect($('#' + col)).to.have.lengthOf(1);
}
});
it('Ignores characters inside of strings', async () => {
const html = await fixture.readFile('/strings/index.html');
const $ = cheerio.load(html);
for (let col of ['red', 'yellow', 'blue']) {
expect($('#' + col)).to.have.lengthOf(1);
}
});
it('Ignores characters inside of line comments', async () => {
const html = await fixture.readFile('/line-comments/index.html');
const $ = cheerio.load(html);
for (let col of ['red', 'yellow', 'blue']) {
expect($('#' + col)).to.have.lengthOf(1);
}
});
it('Ignores characters inside of multiline comments', async () => {
const html = await fixture.readFile('/multiline-comments/index.html');
const $ = cheerio.load(html);
for (let col of ['red', 'yellow', 'blue']) {
expect($('#' + col)).to.have.lengthOf(1);
}
});
it('Allows multiple JSX children in mustache', async () => {
const html = await fixture.readFile('/multiple-children/index.html');
expect(html).to.include('#f');
expect(html).not.to.include('#t');
});
it('Allows <> Fragments in expressions', async () => {
const html = await fixture.readFile('/multiple-children/index.html');
const $ = cheerio.load(html);
expect($('#fragment').children()).to.have.lengthOf(3);
expect($('#fragment').children('#a')).to.have.lengthOf(1);
expect($('#fragment').children('#b')).to.have.lengthOf(1);
expect($('#fragment').children('#c')).to.have.lengthOf(1);
});
it('Does not render falsy values using &&', async () => {
const html = await fixture.readFile('/falsy/index.html');
const $ = cheerio.load(html);
// test 1: Expected {true && <span id="true" />} to render
expect($('#true')).to.have.lengthOf(1);
// test 2: Expected {0 && "VALUE"} to render "0"
expect($('#zero').text()).to.equal('0');
// test 3: Expected {false && <span id="false" />} not to render
expect($('#false')).to.have.lengthOf(0);
// test 4: Expected {null && <span id="null" />} not to render
expect($('#null')).to.have.lengthOf(0);
// test 5: Expected {undefined && <span id="undefined" />} not to render
expect($('#undefined')).to.have.lengthOf(0);
// Inside of a component
// test 6: Expected {true && <span id="true" />} to render
expect($('#frag-true')).to.have.lengthOf(1);
// test 7: Expected {false && <span id="false" />} not to render
expect($('#frag-false')).to.have.lengthOf(0);
// test 8: Expected {null && <span id="null" />} not to render
expect($('#frag-null')).to.have.lengthOf(0);
// test 9: Expected {undefined && <span id="undefined" />} not to render
expect($('#frag-undefined')).to.have.lengthOf(0);
});
it('Escapes HTML by default', async () => {
const html = await fixture.readFile('/escape/index.html');
const $ = cheerio.load(html);
expect($('body').children()).to.have.lengthOf(2);
expect($('body').html()).to.include('&lt;script&gt;console.log("pwnd")&lt;/script&gt;');
expect($('#trusted')).to.have.lengthOf(1);
});
it('Does not double-escape HTML', async () => {
const html = await fixture.readFile('/escape/index.html');
const $ = cheerio.load(html);
expect($('#single-escape').html()).to.equal('Astro &amp; Vite');
});
});