You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
ts-puzzles/sitemap-parser/src/createSitemap.spec.ts

73 lines
2.5 KiB

import { createSitemap } from './createSitemap';
import { ApiResponse, Sitemap } from './types';
describe('createSitemap', () => {
it('creates correct sitemap for sample data', () => {
const sampleInput = [
{ id: 1, slug: 'root', parent: null },
{ id: 2, slug: 'contact', parent: 1 },
{ id: 3, slug: 'email', parent: 2 },
{ id: 4, slug: 'phone', parent: 2 },
{ id: 5, slug: 'products', parent: 1 },
{ id: 6, slug: 'electronics', parent: 5 },
{ id: 7, slug: 'cameras', parent: 6 },
{ id: 8, slug: 'books', parent: 1 },
{ id: 9, slug: 'fiction', parent: 8 },
{ id: 10, slug: 'children', parent: 8 },
] as const satisfies ApiResponse;
const sampleOutput = {
name: 'root',
id: 1,
children: [
{
name: 'contact',
id: 2,
children: [
{
name: 'email',
id: 3,
children: [],
},
{
name: 'phone',
id: 4,
children: [],
},
],
},
{
name: 'products',
id: 5,
children: [
{
name: 'electronics',
id: 6,
children: [
{
name: 'cameras',
id: 7,
children: [],
},
],
},
],
},
{
name: 'books',
id: 8,
children: [
{ name: 'fiction', id: 9, children: [] },
{ name: 'children', id: 10, children: [] },
],
},
],
} as const satisfies Sitemap;
// ideally, for full TS experience, if I had more time, I'd do it so that
// typeof createSitemap(sampleInput) is the same as typeof sampleOutput,
// rather than just a general Sitemap
expect(createSitemap(sampleInput)).toEqual(sampleOutput);
});
});