Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slugify image type #207

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions fixtures/eik-image.json
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
65 changes: 65 additions & 0 deletions test/image.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/// <reference path="../types/index.d.ts" />

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();
},
);