gristlabs_grist-core/test/common/gristUrls.ts
Jarosław Sadziński 69d5ee53a8 (core) Treating API urls as external in cells
Summary:
Links for the API endpoints in a cell didn't work as they were interpreted as
internal routes. Now they are properly detected as external.

Test Plan: Added new test

Reviewers: paulfitz

Reviewed By: paulfitz

Differential Revision: https://phab.getgrist.com/D4078
2023-10-24 08:55:08 +02:00

80 lines
2.6 KiB
TypeScript

import {decodeUrl, IGristUrlState, parseFirstUrlPart} from 'app/common/gristUrls';
import {assert} from 'chai';
describe('gristUrls', function() {
function assertUrlDecode(url: string, expected: Partial<IGristUrlState>) {
const actual = decodeUrl({}, new URL(url));
for (const property in expected) {
const expectedValue = expected[property as keyof IGristUrlState];
const actualValue = actual[property as keyof IGristUrlState];
assert.deepEqual(actualValue, expectedValue);
}
}
describe('encodeUrl', function() {
it('should detect theme appearance override', function() {
assertUrlDecode(
'http://localhost/?themeAppearance=light',
{params: {themeAppearance: 'light'}},
);
assertUrlDecode(
'http://localhost/?themeAppearance=dark',
{params: {themeAppearance: 'dark'}},
);
});
it('should detect theme sync with os override', function() {
assertUrlDecode(
'http://localhost/?themeSyncWithOs=true',
{params: {themeSyncWithOs: true}},
);
});
it('should detect theme name override', function() {
assertUrlDecode(
'http://localhost/?themeName=GristLight',
{params: {themeName: 'GristLight'}},
);
assertUrlDecode(
'http://localhost/?themeName=GristDark',
{params: {themeName: 'GristDark'}},
);
});
it('should detect API URLs', function() {
assertUrlDecode(
'http://localhost/o/docs/api/docs',
{api: true},
);
assertUrlDecode(
'http://public.getgrist.com/api/docs',
{api: true},
);
});
});
describe('parseFirstUrlPart', function() {
it('should strip out matching tag', function() {
assert.deepEqual(parseFirstUrlPart('o', '/o/foo/bar?x#y'), {value: 'foo', path: '/bar?x#y'});
assert.deepEqual(parseFirstUrlPart('o', '/o/foo?x#y'), {value: 'foo', path: '/?x#y'});
assert.deepEqual(parseFirstUrlPart('o', '/o/foo#y'), {value: 'foo', path: '/#y'});
assert.deepEqual(parseFirstUrlPart('o', '/o/foo'), {value: 'foo', path: '/'});
});
it('should pass unchanged non-matching path or tag', function() {
assert.deepEqual(parseFirstUrlPart('xxx', '/o/foo/bar?x#y'), {path: '/o/foo/bar?x#y'});
assert.deepEqual(parseFirstUrlPart('o', '/O/foo/bar?x#y'), {path: '/O/foo/bar?x#y'});
assert.deepEqual(parseFirstUrlPart('o', '/bar?x#y'), {path: '/bar?x#y'});
assert.deepEqual(parseFirstUrlPart('o', '/o/?x#y'), {path: '/o/?x#y'});
assert.deepEqual(parseFirstUrlPart('o', '/#y'), {path: '/#y'});
assert.deepEqual(parseFirstUrlPart('o', ''), {path: ''});
});
});
});