CustomHTMLElements.utils.test.tsx98 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { getAnchor, removeAnchor } from './CustomHTMLElements.utils' |
| 4 | |
| 5 | describe('CustomHTMLElementsUtils', () => { |
| 6 | describe('getAnchor', () => { |
| 7 | describe('when value is an object', () => { |
| 8 | it('returns slugified version of props.children', () => { |
| 9 | const value = { |
| 10 | props: { |
| 11 | children: 'Full Text Search', |
| 12 | }, |
| 13 | } |
| 14 | |
| 15 | const result = getAnchor(value) |
| 16 | |
| 17 | expect(result).toStrictEqual('full-text-search') |
| 18 | }) |
| 19 | }) |
| 20 | |
| 21 | describe('when value is an array', () => { |
| 22 | describe('when custom anchor exists', () => { |
| 23 | it('returns inner slug', () => { |
| 24 | const value = ['Proximity: <->', '[#proximity]'] |
| 25 | |
| 26 | const result = getAnchor(value) |
| 27 | |
| 28 | expect(result).toStrictEqual('proximity') |
| 29 | }) |
| 30 | |
| 31 | it('trims whitespace', () => { |
| 32 | const value = ['Proximity: <->', ' [#proximity] '] |
| 33 | |
| 34 | const result = getAnchor(value) |
| 35 | |
| 36 | expect(result).toStrictEqual('proximity') |
| 37 | }) |
| 38 | }) |
| 39 | |
| 40 | it('returns concatenated slug of elements', () => { |
| 41 | const value = ['Full', 'Text', 'Search'] |
| 42 | |
| 43 | const result = getAnchor(value) |
| 44 | |
| 45 | expect(result).toStrictEqual('full-text-search') |
| 46 | }) |
| 47 | |
| 48 | it('trims whitespace', () => { |
| 49 | const value = [' Full ', ' Text ', ' Search '] |
| 50 | |
| 51 | const result = getAnchor(value) |
| 52 | |
| 53 | expect(result).toStrictEqual('full-text-search') |
| 54 | }) |
| 55 | |
| 56 | it('removes special characters', () => { |
| 57 | const value = ['function()'] |
| 58 | |
| 59 | const result = getAnchor(value) |
| 60 | |
| 61 | expect(result).toStrictEqual('function') |
| 62 | }) |
| 63 | }) |
| 64 | |
| 65 | describe('when value is a string', () => { |
| 66 | it('returns slugified version of string', () => { |
| 67 | const value = 'My (Very) Awesome Heading' |
| 68 | |
| 69 | const result = getAnchor(value) |
| 70 | |
| 71 | expect(result).toStrictEqual('my-very-awesome-heading') |
| 72 | }) |
| 73 | }) |
| 74 | }) |
| 75 | |
| 76 | describe('removeAnchor', () => { |
| 77 | describe('when value is an array', () => { |
| 78 | it('filters out custom anchor elements', () => { |
| 79 | const value = ['My (Very) Awesome Heading', '[#my-custom-heading]'] |
| 80 | |
| 81 | const result = removeAnchor(value) |
| 82 | |
| 83 | expect(result).toStrictEqual(['My (Very) Awesome Heading']) |
| 84 | }) |
| 85 | }) |
| 86 | |
| 87 | describe('when value is a string', () => { |
| 88 | it('strips out custom anchor string', () => { |
| 89 | const value = 'My (Very) Awesome Heading [#my-custom-heading]' |
| 90 | |
| 91 | const result = removeAnchor(value) |
| 92 | |
| 93 | // Original implementation didn't trim the resulting string - not sure if it really matters |
| 94 | expect(result).toStrictEqual('My (Very) Awesome Heading ') |
| 95 | }) |
| 96 | }) |
| 97 | }) |
| 98 | }) |