From c3b4a435c80d6b6a266ee9875b278b2263932da2 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Wed, 16 Oct 2024 14:50:53 +0200 Subject: [PATCH 1/2] test: add failing test case --- fixtures/eik-image.json | 8 +++++ test/image.test.js | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 fixtures/eik-image.json create mode 100644 test/image.test.js diff --git a/fixtures/eik-image.json b/fixtures/eik-image.json new file mode 100644 index 0000000..f7887a3 --- /dev/null +++ b/fixtures/eik-image.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://raw.githubusercontent.com/eik-lib/common/main/lib/schemas/eikjson.schema.json", + "name": "eik-image-fixture", + "version": "1.0.0", + "server": "https://cdn.eik.dev", + "type": "image", + "files": "./images" +} diff --git a/test/image.test.js b/test/image.test.js new file mode 100644 index 0000000..8400c68 --- /dev/null +++ b/test/image.test.js @@ -0,0 +1,65 @@ +/// + +import tap from "tap"; + +import Eik from "../src/index.js"; + +const FIXTURE_PATH = `${process.cwd()}/fixtures/eik-image.json`; + +tap.test("image namespace is mapped correctly to /img", async (t) => { + const client = new Eik({ + base: "/public/images", + path: FIXTURE_PATH, + }); + await client.load(); + + t.equal("/img/eik-image-fixture/1.0.0", client.pathname); +}); + +tap.test("image asset is mapped correctly", async (t) => { + const client = new Eik({ + path: FIXTURE_PATH, + }); + await client.load(); + + const file = "/art/such.webp"; + const resolved = client.file(file); + + t.equal( + resolved.value, + `${client.server}/img/eik-image-fixture/1.0.0/art/such.webp`, + ); + t.end(); +}); + +tap.test('development mode is set to "true" - base is unset', async (t) => { + const client = new Eik({ + development: true, + path: FIXTURE_PATH, + }); + await client.load(); + + const file = "/art/such.webp"; + const resolved = client.file(file); + + t.equal(resolved.value, "/art/such.webp"); + t.end(); +}); + +tap.test( + 'Cdevelopment mode is set to "true" - base is set to absolute URL', + async (t) => { + const client = new Eik({ + development: true, + base: "http://localhost:7777/img/", + path: FIXTURE_PATH, + }); + await client.load(); + + const file = "/art/such.webp"; + const resolved = client.file(file); + + t.equal(resolved.value, "http://localhost:7777/img/art/such.webp"); + t.end(); + }, +); From 6a1959c8502c5f6a617e76278876fd3da978e4b5 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Wed, 16 Oct 2024 14:52:16 +0200 Subject: [PATCH 2/2] fix: slugify image type --- src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 2921d22..4af78b3 100644 --- a/src/index.js +++ b/src/index.js @@ -186,7 +186,12 @@ export default class Eik { */ get pathname() { if (this.#config.type && this.#config.name && this.#config.version) - return join("/", this.type, this.name, this.version).replace(/\\/g, "/"); + return join( + "/", + helpers.typeSlug(this.type), + this.name, + this.version, + ).replace(/\\/g, "/"); throw new Error("Eik config was not loaded before calling .pathname"); }