import { replaceIDs } from '../lib/svg/id';
describe('Testing replaceIDs', () => {
test('Simple code', () => {
const body =
'';
const expected =
'';
// Using callback
let counter = 0;
expect(replaceIDs(body, () => 'callback-' + counter++)).toBe(expected);
});
test('Many IDs', () => {
const body =
'';
// Counter starts with 0, so first id is test-0
expect(replaceIDs(body, 'test-')).toBe(
''
);
// Using callback
let counter = 0;
expect(replaceIDs(body, () => 'callback-' + counter++)).toBe(
body
.replace(/ssvg-id-1st-place-medala/g, 'callback-0')
.replace(/ssvg-id-1st-place-medalb/g, 'callback-7')
.replace(/ssvg-id-1st-place-medalc/g, 'callback-10')
.replace(/ssvg-id-1st-place-medald/g, 'callback-1')
.replace(/ssvg-id-1st-place-medale/g, 'callback-11')
.replace(/ssvg-id-1st-place-medalf/g, 'callback-2')
.replace(/ssvg-id-1st-place-medalg/g, 'callback-12')
.replace(/ssvg-id-1st-place-medalh/g, 'callback-3')
.replace(/ssvg-id-1st-place-medali/g, 'callback-13')
.replace(/ssvg-id-1st-place-medalj/g, 'callback-4')
.replace(/ssvg-id-1st-place-medalk/g, 'callback-8')
.replace(/ssvg-id-1st-place-medall/g, 'callback-14')
.replace(/ssvg-id-1st-place-medalm/g, 'callback-5')
.replace(/ssvg-id-1st-place-medaln/g, 'callback-15')
.replace(/ssvg-id-1st-place-medalo/g, 'callback-9')
.replace(/ssvg-id-1st-place-medalp/g, 'callback-6')
);
});
test('Matching parts', () => {
const body =
'';
const expected =
'';
expect(
replaceIDs(body, (id: string) => {
switch (id) {
case 'test1':
return 'callback-0';
case 'test':
return 'callback-1';
default:
throw new Error(`Unexpected id "${id}""`);
}
})
).toBe(expected);
});
test('With animation', () => {
const body =
'';
// To avoid messing up counter, using custom callback
let counter = 0;
expect(replaceIDs(body, () => 'callback-' + counter++)).toBe(
body
.replace(/ssvg-id-compassa/g, 'callback-2')
.replace(/ssvg-id-compassb/g, 'callback-0')
.replace(/ssvg-id-compassc/g, 'callback-1')
);
});
test('Matching parts in attributes, tags and content', () => {
// 'path' matches tag -> callback-0
// 'M12' matches path value -> callback-1
// 'd' matches attribute -> callback-2
const body =
'';
const expected =
'';
// Using custom callback for reliable results
expect(
replaceIDs(body, (id: string) => {
switch (id) {
case 'path':
return 'callback-0';
case 'M12':
return 'callback-1';
case 'd':
return 'callback-2';
default:
throw new Error(`Unexpected id: ${id}`);
}
})
).toBe(expected);
});
});