refactor: only include template in share url if it's custom
This commit is contained in:
parent
bb429d8195
commit
be11c0e57a
6 changed files with 249 additions and 32 deletions
|
|
@ -5,12 +5,25 @@ import {
|
|||
encodeTemplateURL,
|
||||
decodeTemplateURL
|
||||
} from './sharing';
|
||||
import { presets } from './presets';
|
||||
import type { Character, Template } from './types';
|
||||
|
||||
const testTemplate: Template = {
|
||||
id: 'preset:standard',
|
||||
name: 'Standard',
|
||||
description: 'The standard record format.',
|
||||
const standardPreset = presets.find((p) => p.id === 'preset:standard')!;
|
||||
|
||||
const testCharacter: Character = {
|
||||
id: 'abc-123',
|
||||
template: standardPreset,
|
||||
data: {
|
||||
name: 'Yury Zakharov',
|
||||
species: 'human',
|
||||
'employment-history': 'Shaft Miner'
|
||||
}
|
||||
};
|
||||
|
||||
const customTemplate: Template = {
|
||||
id: 'custom:test',
|
||||
name: 'Custom',
|
||||
description: 'A custom template.',
|
||||
schemaVersion: 1,
|
||||
records: [
|
||||
{
|
||||
|
|
@ -23,35 +36,34 @@ const testTemplate: Template = {
|
|||
]
|
||||
};
|
||||
|
||||
const testCharacter: Character = {
|
||||
id: 'abc-123',
|
||||
template: testTemplate,
|
||||
data: {
|
||||
name: 'Yury Zakharov',
|
||||
species: 'human',
|
||||
'employment-history': 'Shaft Miner'
|
||||
}
|
||||
};
|
||||
|
||||
describe('character URL encoding', () => {
|
||||
it('round-trips character data', () => {
|
||||
it('round-trips preset character data', () => {
|
||||
const encoded = encodeCharacterURL(testCharacter);
|
||||
const decoded = decodeCharacterURL(encoded);
|
||||
expect(decoded.data).toEqual(testCharacter.data);
|
||||
expect(decoded.template.name).toBe('Standard');
|
||||
});
|
||||
|
||||
it('uses short encoding for preset templates', () => {
|
||||
const encoded = encodeCharacterURL(testCharacter);
|
||||
const customChar = { ...testCharacter, template: customTemplate };
|
||||
const customEncoded = encodeCharacterURL(customChar);
|
||||
expect(encoded.length).toBeLessThan(customEncoded.length);
|
||||
});
|
||||
|
||||
it('round-trips custom template character', () => {
|
||||
const char: Character = { ...testCharacter, template: customTemplate };
|
||||
const encoded = encodeCharacterURL(char);
|
||||
const decoded = decodeCharacterURL(encoded);
|
||||
expect(decoded.data).toEqual(testCharacter.data);
|
||||
expect(decoded.template.name).toBe('Custom');
|
||||
});
|
||||
|
||||
it('starts with c1. prefix', () => {
|
||||
const encoded = encodeCharacterURL(testCharacter);
|
||||
expect(encoded.startsWith('c1.')).toBe(true);
|
||||
});
|
||||
|
||||
it('strips id', () => {
|
||||
const encoded = encodeCharacterURL(testCharacter);
|
||||
const decoded = decodeCharacterURL(encoded);
|
||||
expect(decoded).not.toHaveProperty('id');
|
||||
});
|
||||
|
||||
it('prunes empty values from data', () => {
|
||||
const char: Character = {
|
||||
...testCharacter,
|
||||
|
|
@ -65,19 +77,19 @@ describe('character URL encoding', () => {
|
|||
|
||||
describe('template URL encoding', () => {
|
||||
it('round-trips template structure', () => {
|
||||
const encoded = encodeTemplateURL(testTemplate);
|
||||
const encoded = encodeTemplateURL(customTemplate);
|
||||
const decoded = decodeTemplateURL(encoded);
|
||||
expect(decoded.name).toBe('Standard');
|
||||
expect(decoded.records).toEqual(testTemplate.records);
|
||||
expect(decoded.name).toBe('Custom');
|
||||
expect(decoded.records).toEqual(customTemplate.records);
|
||||
});
|
||||
|
||||
it('starts with t1. prefix', () => {
|
||||
const encoded = encodeTemplateURL(testTemplate);
|
||||
const encoded = encodeTemplateURL(customTemplate);
|
||||
expect(encoded.startsWith('t1.')).toBe(true);
|
||||
});
|
||||
|
||||
it('strips id', () => {
|
||||
const encoded = encodeTemplateURL(testTemplate);
|
||||
const encoded = encodeTemplateURL(customTemplate);
|
||||
const decoded = decodeTemplateURL(encoded);
|
||||
expect(decoded).not.toHaveProperty('id');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue