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/resolveByPath.spec.ts

36 lines
1.6 KiB

import { createSitemap } from './createSitemap';
import { createPathResolver } from './resolveByPath';
describe('resolveByPath', () => {
it('resolves sample paths correctly', () => {
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 },
];
const sitemap = createSitemap(sampleInput);
// ideally, for full TS experience, if I had more time, I'd do it so that resolvePath declaration
// makes it so that auto-complete for path works here, and e.g.
// the return value of `resolveByPath('/contact')` is declared not just as `number | null` but as `2`.
const resolveByPath = createPathResolver(sitemap);
expect(resolveByPath('/')).toBe(1);
expect(resolveByPath('/contact')).toBe(2);
expect(resolveByPath('/contact/email')).toBe(3);
expect(resolveByPath('/contact/whatsapp')).toBe(null);
expect(resolveByPath('/products/electronics/cameras')).toBe(7);
// It is not clear which one of these two is considered to be correct
//expect(resolveByPath('products///electronics///cameras///')).toBe(7);
expect(resolveByPath('products///electronics///cameras///')).toBe(null);
});
});