njsparser/js/tests/api.test.js
2026-02-15 00:12:22 +01:00

96 lines
3.4 KiB
JavaScript

import { test, expect } from 'bun:test';
import {
getApiPath,
getIndexApiPath,
isApiExposedFromResponse,
listApiPaths
} from '../api.js';
test('getApiPath generates correct path', () => {
const path = getApiPath('abc123', '', '/page');
expect(path).toBe('/_next/data/abc123/page.json');
});
test('getApiPath with base path', () => {
const path = getApiPath('abc123', '/base', '/page');
expect(path).toBe('/base/_next/data/abc123/page.json');
});
test('getApiPath defaults to index.json', () => {
const path = getApiPath('abc123');
expect(path).toBe('/_next/data/abc123/index.json');
});
test('getApiPath appends .json extension', () => {
const path = getApiPath('abc123', '', '/page');
expect(path).toBe('/_next/data/abc123/page.json');
});
test('getApiPath handles path ending with /', () => {
const path = getApiPath('abc123', '', '/page/');
expect(path).toBe('/_next/data/abc123/index.json');
});
test('getApiPath returns null for excluded paths', () => {
expect(getApiPath('abc123', '', '/404')).toBeNull();
expect(getApiPath('abc123', '', '/_app')).toBeNull();
expect(getApiPath('abc123', '', '/_error')).toBeNull();
expect(getApiPath('abc123', '', '/sitemap.xml')).toBeNull();
expect(getApiPath('abc123', '', '/_middleware')).toBeNull();
});
test('getIndexApiPath generates index path', () => {
const path = getIndexApiPath('abc123');
expect(path).toBe('/_next/data/abc123/index.json');
});
test('getIndexApiPath with base path', () => {
const path = getIndexApiPath('abc123', '/base');
expect(path).toBe('/base/_next/data/abc123/index.json');
});
test('isApiExposedFromResponse returns true for 200 status', () => {
expect(isApiExposedFromResponse(200, null, '')).toBe(true);
});
test('isApiExposedFromResponse returns true for application/json', () => {
expect(isApiExposedFromResponse(404, 'application/json', '')).toBe(true);
expect(isApiExposedFromResponse(404, 'application/json; charset=utf-8', '')).toBe(true);
});
test('isApiExposedFromResponse returns true for notFound response', () => {
expect(isApiExposedFromResponse(404, 'text/html', '{"notFound":true}')).toBe(true);
});
test('isApiExposedFromResponse returns false otherwise', () => {
expect(isApiExposedFromResponse(404, 'text/html', 'Not found')).toBe(false);
});
test('listApiPaths generates paths for all pages', () => {
const sortedPages = ['/page1', '/page2', '/page3'];
const paths = listApiPaths(sortedPages, 'abc123', '');
expect(paths).toHaveLength(3);
expect(paths[0]).toBe('/_next/data/abc123/page1.json');
expect(paths[1]).toBe('/_next/data/abc123/page2.json');
expect(paths[2]).toBe('/_next/data/abc123/page3.json');
});
test('listApiPaths filters excluded paths', () => {
const sortedPages = ['/page1', '/404', '/_app', '/page2'];
const paths = listApiPaths(sortedPages, 'abc123', '');
expect(paths).toHaveLength(2);
expect(paths[0]).toBe('/_next/data/abc123/page1.json');
expect(paths[1]).toBe('/_next/data/abc123/page2.json');
});
test('listApiPaths returns empty array when API not exposed', () => {
const sortedPages = ['/page1', '/page2'];
const paths = listApiPaths(sortedPages, 'abc123', '', false);
expect(paths).toHaveLength(0);
});
test('listApiPaths with base path', () => {
const sortedPages = ['/page1'];
const paths = listApiPaths(sortedPages, 'abc123', '/base');
expect(paths[0]).toBe('/base/_next/data/abc123/page1.json');
});