From 2ff0715f9a67fbbce4a1efeaf069755d0ffcb4ea Mon Sep 17 00:00:00 2001 From: Michael Stramel Date: Wed, 25 Sep 2024 15:39:35 -0500 Subject: [PATCH] test: add supporting tests --- packages/astro/test/core-image-svg.test.js | 269 ++++++++++++++++++ .../test/fixtures/core-image-svg/package.json | 11 + .../src/assets/alpine-multi-color.svg | 10 + .../core-image-svg/src/assets/astro.svg | 4 + .../src/assets/chevron-right.svg | 3 + .../core-image-svg/src/assets/github.svg | 6 + .../core-image-svg/src/assets/penguin.svg | 1 + .../core-image-svg/src/assets/plus.svg | 3 + .../core-image-svg/src/pages/index.astro | 17 ++ .../core-image-svg/src/pages/inline.astro | 22 ++ .../core-image-svg/src/pages/multiple.astro | 27 ++ .../core-image-svg/src/pages/props.astro | 13 + .../core-image-svg/src/pages/size.astro | 26 ++ .../core-image-svg/src/pages/strip.astro | 13 + .../core-image-svg/src/pages/title.astro | 13 + .../fixtures/core-image-svg/tsconfig.json | 11 + pnpm-lock.yaml | 6 + 17 files changed, 455 insertions(+) create mode 100644 packages/astro/test/core-image-svg.test.js create mode 100644 packages/astro/test/fixtures/core-image-svg/package.json create mode 100644 packages/astro/test/fixtures/core-image-svg/src/assets/alpine-multi-color.svg create mode 100644 packages/astro/test/fixtures/core-image-svg/src/assets/astro.svg create mode 100644 packages/astro/test/fixtures/core-image-svg/src/assets/chevron-right.svg create mode 100644 packages/astro/test/fixtures/core-image-svg/src/assets/github.svg create mode 100644 packages/astro/test/fixtures/core-image-svg/src/assets/penguin.svg create mode 100644 packages/astro/test/fixtures/core-image-svg/src/assets/plus.svg create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/index.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/inline.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/multiple.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/props.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/size.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/strip.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/src/pages/title.astro create mode 100644 packages/astro/test/fixtures/core-image-svg/tsconfig.json diff --git a/packages/astro/test/core-image-svg.test.js b/packages/astro/test/core-image-svg.test.js new file mode 100644 index 0000000000000..76efba9482c67 --- /dev/null +++ b/packages/astro/test/core-image-svg.test.js @@ -0,0 +1,269 @@ +import assert from 'node:assert/strict'; +import { basename } from 'node:path'; +import { Writable } from 'node:stream'; +import { after, before, describe, it } from 'node:test'; +import { removeDir } from '@astrojs/internal-helpers/fs'; +import * as cheerio from 'cheerio'; +import parseSrcset from 'parse-srcset'; +import { Logger } from '../dist/core/logger/core.js'; +import testAdapter from './test-adapter.js'; +import { testImageService } from './test-image-service.js'; +import { loadFixture } from './test-utils.js'; + +describe('astro:svg - SVG Components', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + describe('dev', () => { + /** @type {import('./test-utils').DevServer} */ + let devServer; + /** @type {Array<{ type: any, level: 'error', message: string; }>} */ + let logs = []; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/core-image-svg/', + }); + + devServer = await fixture.startDevServer({ + logger: new Logger({ + level: 'error', + dest: new Writable({ + objectMode: true, + write(event, _, callback) { + logs.push(event); + callback(); + }, + }), + }), + }); + }); + + after(async () => { + await devServer.stop(); + }); + + describe('basics', () => { + let $; + before(async () => { + let res = await fixture.fetch('/'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('Adds the tag with the definition', () => { + const $svg = $('#definition svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('role'), 'img'); + + const $symbol = $svg.find('symbol'); + assert.equal($symbol.length, 1); + assert.equal($symbol.attr('id').startsWith('a:'), true); + + const $use = $svg.find('symbol + use'); + assert.equal($use.length, 1); + assert.equal($use.attr('xlink:href').startsWith('#a:'), true); + assert.equal($use.attr('xlink:href').slice(1), $symbol.attr('id')); + }); + it('Adds the tag that uses the definition', () => { + let $svg = $('#reused svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('role'), 'img'); + + const $symbol = $svg.find('symbol'); + assert.equal($symbol.length, 0); + + const definitionId = $('#definition svg symbol').attr('id') + const $use = $svg.find('use'); + assert.equal($use.length, 1); + assert.equal($use.attr('xlink:href').startsWith('#a:'), true); + assert.equal($use.attr('xlink:href').slice(1), definitionId); + }); + }); + + describe('props', () => { + describe('size', () => { + let $; + before(async () => { + let res = await fixture.fetch('/size'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('has no height and width - no dimensions set', () => { + let $svg = $('#base svg'); + assert.equal($svg.length, 1); + assert.equal(!!$svg.attr('height'), false); + assert.equal(!!$svg.attr('width'), false); + }); + it('has height and width - no dimensions set', () => { + let $svg = $('#base-with-defaults svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('height'), '1em'); + assert.equal($svg.attr('width'), '1em'); + }); + it('has height and width - string size set', () => { + let $svg = $('#size-string svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('height'), '32'); + assert.equal($svg.attr('width'), '32'); + assert.equal(!!$svg.attr('size'), false); + }); + it('has height and width - number size set', () => { + let $svg = $('#size-number svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('height'), '48'); + assert.equal($svg.attr('width'), '48'); + assert.equal(!!$svg.attr('size'), false); + }); + it('has height and width overridden - size set', () => { + let $svg = $('#override-attrs svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('height'), '16'); + assert.equal($svg.attr('width'), '16'); + assert.equal(!!$svg.attr('size'), false); + }); + }); + describe('inline', () => { + let $; + before(async () => { + let res = await fixture.fetch('/inline'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('adds the svg into the document directly', () => { + let $svg = $('#inline svg'); + assert.equal($svg.length, 1); + assert.equal(!!$svg.attr('viewBox'), true); + assert.equal($svg.attr('height'), '1em'); + assert.equal($svg.attr('width'), '1em'); + assert.equal($svg.attr('role'), 'img'); + assert.equal(!!$svg.attr('inline'), false); + + const $symbol = $svg.find('symbol') + assert.equal($symbol.length, 0); + const $use = $svg.find('use') + assert.equal($use.length, 0); + const $path = $svg.find('path'); + assert.equal($path.length, 1); + }); + it('adds the svg into the document and overrides the dimensions', () => { + let $svg = $('#inline-with-size svg'); + assert.equal($svg.length, 1); + assert.equal(!!$svg.attr('viewBox'), true); + assert.equal($svg.attr('height'), '24'); + assert.equal($svg.attr('width'), '24'); + assert.equal($svg.attr('role'), 'img'); + assert.equal(!!$svg.attr('inline'), false); + + const $symbol = $svg.find('symbol') + assert.equal($symbol.length, 0); + const $use = $svg.find('use') + assert.equal($use.length, 0); + const $path = $svg.find('path'); + assert.equal($path.length, 1); + }) + }); + describe('title', () => { + let $; + before(async () => { + let res = await fixture.fetch('/title'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('adds a title into the SVG', () => { + let $svg = $('#base svg'); + assert.equal($svg.length, 1); + assert.equal(!!$svg.attr('title'), false); + + const $title = $('#base svg > title'); + assert.equal($title.length, 1); + assert.equal($title.text(), 'GitHub Logo') + }); + }); + describe('strip', () => { + let $; + before(async () => { + let res = await fixture.fetch('/strip'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('removes unnecessary attributes', () => { + let $svg = $('#base svg'); + assert.equal($svg.length, 1); + assert.equal(!!$svg.attr('xmlns'), false); + assert.equal(!!$svg.attr('xmlns:xlink'), false); + assert.equal(!!$svg.attr('version'), false); + }); + }); + describe('additional props', () => { + let $; + before(async () => { + let res = await fixture.fetch('/props'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('adds the svg into the document directly', () => { + let $svg = $('#base svg'); + assert.equal($svg.length, 1); + assert.equal($svg.attr('aria-hidden'), 'true'); + assert.equal($svg.attr('id'), 'plus'); + assert.equal($svg.attr('style'), `color:red;font-size:32px`); + assert.equal($svg.attr('class'), 'foobar'); + assert.equal($svg.attr('data-state'), 'open'); + + const $symbol = $svg.find('symbol') + assert.equal($symbol.length, 1); + const $use = $svg.find('use') + assert.equal($use.length, 1); + }); + }); + }); + + describe('multiple', () => { + let $; + before(async () => { + let res = await fixture.fetch('/multiple'); + let html = await res.text(); + $ = cheerio.load(html, { xml: true }); + }); + + it('adds only one definition for each svg', () => { + // First SVG + let $svg = $('.one svg'); + assert.equal($svg.length, 2); + let $symbol = $('.one svg > symbol'); + assert.equal($symbol.length, 1); + let $use = $('.one svg > use'); + assert.equal($use.length, 2); + let defId = $('.one.def svg > use').attr('id'); + let useId = $('.one.use svg > use').attr('id'); + assert.equal(defId, useId); + + // Second SVG + $svg = $('.two svg'); + assert.equal($svg.length, 2); + $symbol = $('.two svg > symbol'); + assert.equal($symbol.length, 1); + $use = $('.two svg > use'); + assert.equal($use.length, 2); + defId = $('.two.def svg > use').attr('id'); + useId = $('.two.use svg > use').attr('id'); + assert.equal(defId, useId); + + + // Third SVG + $svg = $('.three svg'); + assert.equal($svg.length, 1); + $symbol = $('.three svg > symbol'); + assert.equal($symbol.length, 1); + $use = $('.three svg > use'); + assert.equal($use.length, 1); + }); + }); + }); +}); diff --git a/packages/astro/test/fixtures/core-image-svg/package.json b/packages/astro/test/fixtures/core-image-svg/package.json new file mode 100644 index 0000000000000..a4c2fd9e38b73 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/package.json @@ -0,0 +1,11 @@ +{ + "name": "@test/core-image-svg", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + }, + "scripts": { + "dev": "astro dev" + } +} diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/alpine-multi-color.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/alpine-multi-color.svg new file mode 100644 index 0000000000000..3654564da09bf --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/alpine-multi-color.svg @@ -0,0 +1,10 @@ + + Custom Preset 4 Copy 5 + + + + + + + + diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/astro.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/astro.svg new file mode 100644 index 0000000000000..f762da0f6e569 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/astro.svg @@ -0,0 +1,4 @@ + + + + diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/chevron-right.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/chevron-right.svg new file mode 100644 index 0000000000000..3fcc67a0e52b9 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/chevron-right.svg @@ -0,0 +1,3 @@ + + + diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/github.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/github.svg new file mode 100644 index 0000000000000..ce45f4c95b3ec --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/github.svg @@ -0,0 +1,6 @@ + + + diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/penguin.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/penguin.svg new file mode 100644 index 0000000000000..d93379b6846fe --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/penguin.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/astro/test/fixtures/core-image-svg/src/assets/plus.svg b/packages/astro/test/fixtures/core-image-svg/src/assets/plus.svg new file mode 100644 index 0000000000000..d0f57e4f60be5 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/assets/plus.svg @@ -0,0 +1,3 @@ + + + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/index.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/index.astro new file mode 100644 index 0000000000000..92599b9595a42 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/index.astro @@ -0,0 +1,17 @@ +--- +import AstroLogo from "~/assets/astro.svg"; +--- + + + + + + +
+ +
+
+ +
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/inline.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/inline.astro new file mode 100644 index 0000000000000..392b7b49e256f --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/inline.astro @@ -0,0 +1,22 @@ +--- +import ChrevronRight from '~/assets/chevron-right.svg' +--- + + + + + +
+ +
+
+ +
+
+ +
+
+ +
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/multiple.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/multiple.astro new file mode 100644 index 0000000000000..79577d45b0cc8 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/multiple.astro @@ -0,0 +1,27 @@ +--- +import AstroLogo from '~/assets/astro.svg'; +import GithubLogo from '~/assets/github.svg'; +import AlpineLogo from '~/assets/alpine-multi-color.svg'; +--- + + + + + +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/props.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/props.astro new file mode 100644 index 0000000000000..75567a6db454d --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/props.astro @@ -0,0 +1,13 @@ +--- +import Plus from '~/assets/plus.svg' +--- + + + + + +
+
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/size.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/size.astro new file mode 100644 index 0000000000000..dea4d003070bf --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/size.astro @@ -0,0 +1,26 @@ +--- +import AstroLogo from '~/assets/astro.svg'; +import Plus from '~/assets/plus.svg'; +--- + + + + + +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/strip.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/strip.astro new file mode 100644 index 0000000000000..744b9cc8e1cae --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/strip.astro @@ -0,0 +1,13 @@ +--- +import Alpine from '~/assets/alpine-multi-color.svg' +--- + + + + + +
+ +
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/src/pages/title.astro b/packages/astro/test/fixtures/core-image-svg/src/pages/title.astro new file mode 100644 index 0000000000000..eb3b5f11d65a5 --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/src/pages/title.astro @@ -0,0 +1,13 @@ +--- +import GitHub from '~/assets/github.svg' +--- + + + + + +
+ +
+ + diff --git a/packages/astro/test/fixtures/core-image-svg/tsconfig.json b/packages/astro/test/fixtures/core-image-svg/tsconfig.json new file mode 100644 index 0000000000000..923ed4e24fb7b --- /dev/null +++ b/packages/astro/test/fixtures/core-image-svg/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "astro/tsconfigs/strict", + "compilerOptions": { + "baseUrl": ".", + "paths": { + "~/assets/*": [ + "src/assets/*" + ] + }, + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c6ea2646cb412..c8fe34453a58d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2861,6 +2861,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/core-image-svg: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/core-image-unconventional-settings: dependencies: astro: