Skip to content
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
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
engine-strict=true

1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.18.0
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 22.18.0
3 changes: 3 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tools]
node = "22.18.0"

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"version": "0.0.0",
"type": "module",
"engines": {
"node": ">=20"
"node": "22.18.0"
},
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"test": "vitest run --coverage",
"build:stats": "VITE_VISUALIZER=true vite build"
"build:stats": "VITE_VISUALIZER=true vite build",
"preinstall": "node -e \"const [maj,min]=process.versions.node.split('.').map(Number);if(maj!==22||min<12){console.error('Node 22.12+ required. Detected '+process.versions.node);process.exit(1)}\""
},
"dependencies": {
"dompurify": "^3.2.6",
Expand Down
18 changes: 10 additions & 8 deletions tests/stores/api/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';

import { ApiClient, ApiClientOptions } from '@api/client.ts';
import { HttpError } from '@api/http-error.ts';

Expand All @@ -10,19 +11,20 @@ const options: ApiClientOptions = {

const url = 'http://example.com/';

// Ensure environment variable for base url
process.env.VITE_API_URL = url;

let client: ApiClient;
let nonce: string;

beforeEach(() => {
vi.stubEnv('VITE_API_URL', url);
client = new ApiClient(options);
nonce = 'nonce';
localStorage.clear();
vi.stubGlobal('fetch', vi.fn());
});

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});

describe('ApiClient', () => {
Expand All @@ -39,12 +41,12 @@ describe('ApiClient', () => {
const data = { ok: true };
(fetch as vi.Mock).mockResolvedValue(new Response(JSON.stringify(data), { status: 200 }));

const result = await client.post('test', { id: 1 });
const result = await client.post('test', nonce, { id: 1 });
expect(result).toEqual(data);

(fetch as vi.Mock).mockResolvedValue(new Response('fail', { status: 500, statusText: 'err' }));

await expect(client.post('test', { id: 2 })).rejects.toBeInstanceOf(HttpError);
await expect(client.post('test', nonce, { id: 2 })).rejects.toBeInstanceOf(HttpError);
});

it('caches get requests and serves from cache', async () => {
Expand All @@ -54,7 +56,7 @@ describe('ApiClient', () => {

(fetch as vi.Mock).mockResolvedValue(new Response(null, { status: 304 }));

const result = await client.get('test');
const result = await client.get('test', nonce);
expect(result).toEqual({ cached: true });
});

Expand All @@ -67,14 +69,14 @@ describe('ApiClient', () => {
}),
);

const result = await client.get('test');
const result = await client.get('test', nonce);
expect(result).toEqual(data);
expect(localStorage.getItem('api-cache-test')).not.toBeNull();
});

it('throws HttpError on failed get', async () => {
(fetch as vi.Mock).mockResolvedValue(new Response('nope', { status: 404, statusText: 'NF' }));

await expect(client.get('oops')).rejects.toBeInstanceOf(HttpError);
await expect(client.get('oops', nonce)).rejects.toBeInstanceOf(HttpError);
});
});
Loading