function shallowRender(props: Partial<Sidebar['props']> = {}) {
return shallow(
<Sidebar
- location={{ pathname: '/foo/baz/foo/bar' } as Location}
+ location={{ pathname: '/2.0/foo/baz/foo/bar' } as Location}
pages={[
{
fields: {
key="/foo/"
location={
Object {
- "pathname": "/foo/baz/foo/bar",
+ "pathname": "/2.0/foo/baz/foo/bar",
}
}
node={
key="Foo subs"
location={
Object {
- "pathname": "/foo/baz/foo/bar",
+ "pathname": "/2.0/foo/baz/foo/bar",
}
}
openByDefault={true}
key="Foo Baz subs"
location={
Object {
- "pathname": "/foo/baz/foo/bar",
+ "pathname": "/2.0/foo/baz/foo/bar",
}
}
openByDefault={true}
key="Foo Baz Foo subs"
location={
Object {
- "pathname": "/foo/baz/foo/bar",
+ "pathname": "/2.0/foo/baz/foo/bar",
}
}
openByDefault={true}
key="/bar/"
location={
Object {
- "pathname": "/foo/baz/foo/bar",
+ "pathname": "/2.0/foo/baz/foo/bar",
}
}
node={
key="Bar subs"
location={
Object {
- "pathname": "/foo/baz/foo/bar",
+ "pathname": "/2.0/foo/baz/foo/bar",
}
}
openByDefault={false}
describe('getOpenChainFromPath', () => {
it('should correctly fetch the chain of open elements for a given path', () => {
expect(getOpenChainFromPath('path/value/', navTree)).toEqual([navTree[0]]);
+ expect(getOpenChainFromPath('latest/path/value/', navTree)).toEqual([navTree[0]]);
expect(getOpenChainFromPath('sub/child/path/3', navTree)).toEqual([
navTree[1],
(navTree as any)[1].children[1],
expect(testPathAgainstUrl('path/foo', 'path/foo/')).toBe(true);
expect(testPathAgainstUrl('/path/foo/', 'path/foo')).toBe(true);
expect(testPathAgainstUrl('path/foo', '/path/foo/')).toBe(true);
+ expect(testPathAgainstUrl('/path/foo', '/1.0/path/foo/')).toBe(true);
+ expect(testPathAgainstUrl('/path/foo', '/latest/path/foo/')).toBe(true);
});
});
}
export function testPathAgainstUrl(path: string, url: string) {
+ return trimSlashes(url).replace(/^(latest|\d+\.\d+)\//, '') === trimSlashes(path);
+}
+
+function trimSlashes(string: string) {
const leadingRegEx = /^\//;
const trailingRegEx = /\/$/;
- return (
- path.replace(leadingRegEx, '').replace(trailingRegEx, '') ===
- url.replace(leadingRegEx, '').replace(trailingRegEx, '')
- );
+ return string.replace(leadingRegEx, '').replace(trailingRegEx, '');
}