|
| 1 | +import cases from 'jest-in-case'; |
| 2 | + |
| 3 | +import plugin from '../../'; |
| 4 | +import { getHTML, shouldTransform } from '../../transformers/Lichess'; |
| 5 | + |
| 6 | +import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers'; |
| 7 | + |
| 8 | +cases( |
| 9 | + 'url validation', |
| 10 | + ({ url, valid }) => { |
| 11 | + expect(shouldTransform(url)).toBe(valid); |
| 12 | + }, |
| 13 | + { |
| 14 | + 'non-Lichess url': { |
| 15 | + url: 'https://not-a-lichess-url.org', |
| 16 | + valid: false, |
| 17 | + }, |
| 18 | + "non-Lichess url ending with 'lichess.org'": { |
| 19 | + url: 'https://this-is-not-lichess.org', |
| 20 | + valid: false, |
| 21 | + }, |
| 22 | + "non-Lichess url ending with 'lichess.org' having '/embed/' path": { |
| 23 | + url: 'https://this-is-not-lichess.org/embed/MPJcy1JW', |
| 24 | + valid: false, |
| 25 | + }, |
| 26 | + 'embed game url': { |
| 27 | + url: 'https://lichess.org/embed/MPJcy1JW', |
| 28 | + valid: false, |
| 29 | + }, |
| 30 | + 'learn url': { |
| 31 | + url: 'https://lichess.org/learn', |
| 32 | + valid: false, |
| 33 | + }, |
| 34 | + 'practice url': { |
| 35 | + url: 'https://lichess.org/practice', |
| 36 | + valid: false, |
| 37 | + }, |
| 38 | + 'study list url': { |
| 39 | + url: 'https://lichess.org/study', |
| 40 | + valid: false, |
| 41 | + }, |
| 42 | + 'study url': { |
| 43 | + url: 'https://lichess.org/study/XtFCFYlM', |
| 44 | + valid: false, |
| 45 | + }, |
| 46 | + 'training url': { |
| 47 | + url: 'https://lichess.org/training/12345', |
| 48 | + valid: false, |
| 49 | + }, |
| 50 | + 'tv list url': { |
| 51 | + url: 'https://lichess.org/tv', |
| 52 | + valid: false, |
| 53 | + }, |
| 54 | + 'tv url': { |
| 55 | + url: 'https://lichess.org/tv/best', |
| 56 | + valid: false, |
| 57 | + }, |
| 58 | + 'game url': { |
| 59 | + url: 'https://lichess.org/MPJcy1JW', |
| 60 | + valid: true, |
| 61 | + }, |
| 62 | + "game url having 'www' subdomain": { |
| 63 | + url: 'https://www.lichess.org/MPJcy1JW', |
| 64 | + valid: true, |
| 65 | + }, |
| 66 | + 'game url with parameters': { |
| 67 | + url: 'https://lichess.org/MPJcy1JW?theme=auto&bg=auto', |
| 68 | + valid: true, |
| 69 | + }, |
| 70 | + "game url with parameters having 'www' subdomain": { |
| 71 | + url: 'https://www.lichess.org/MPJcy1JW?theme=auto&bg=auto', |
| 72 | + valid: true, |
| 73 | + }, |
| 74 | + "game url with game-ID starting with 'tv'": { |
| 75 | + url: 'https://lichess.org/tv123abc56de', |
| 76 | + valid: true, |
| 77 | + }, |
| 78 | + "game url with game-ID starting with 'tv' and having 'www' subdomain": { |
| 79 | + url: 'https://www.lichess.org/tv123abc56de', |
| 80 | + valid: true, |
| 81 | + }, |
| 82 | + } |
| 83 | +); |
| 84 | + |
| 85 | +test('Gets the correct Lichess iframe', () => { |
| 86 | + const html = getHTML('https://lichess.org/MPJcy1JW'); |
| 87 | + |
| 88 | + expect(html).toMatchInlineSnapshot( |
| 89 | + `"<iframe src=\\"https://lichess.org/embed/MPJcy1JW\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe>"` |
| 90 | + ); |
| 91 | +}); |
| 92 | + |
| 93 | +test('Plugin can transform Lichess links', async () => { |
| 94 | + const markdownAST = getMarkdownASTForFile('Lichess'); |
| 95 | + |
| 96 | + const processedAST = await plugin({ cache, markdownAST }); |
| 97 | + |
| 98 | + expect(parseASTToMarkdown(processedAST)).toMatchInlineSnapshot(` |
| 99 | + "<https://not-a-lichess-url.org> |
| 100 | +
|
| 101 | + <https://this-is-not-lichess.org> |
| 102 | +
|
| 103 | + <https://this-is-not-lichess.org/embed/MPJcy1JW> |
| 104 | +
|
| 105 | + <https://lichess.org/embed/MPJcy1JW> |
| 106 | +
|
| 107 | + <https://lichess.org/learn> |
| 108 | +
|
| 109 | + <https://lichess.org/practice> |
| 110 | +
|
| 111 | + <https://lichess.org/study> |
| 112 | +
|
| 113 | + <https://lichess.org/study/XtFCFYlM> |
| 114 | +
|
| 115 | + <https://lichess.org/training/12345> |
| 116 | +
|
| 117 | + <https://lichess.org/tv> |
| 118 | +
|
| 119 | + <https://lichess.org/tv/best> |
| 120 | +
|
| 121 | + <iframe src=\\"https://lichess.org/embed/MPJcy1JW\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe> |
| 122 | +
|
| 123 | + <iframe src=\\"https://www.lichess.org/embed/MPJcy1JW\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe> |
| 124 | +
|
| 125 | + <iframe src=\\"https://lichess.org/embed/MPJcy1JW?theme=auto&bg=auto\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe> |
| 126 | +
|
| 127 | + <iframe src=\\"https://www.lichess.org/embed/MPJcy1JW?theme=auto&bg=auto\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe> |
| 128 | +
|
| 129 | + <iframe src=\\"https://lichess.org/embed/tv123abc56de\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe> |
| 130 | +
|
| 131 | + <iframe src=\\"https://www.lichess.org/embed/tv123abc56de\\" width=\\"600\\" height=\\"397\\" frameborder=\\"0\\"></iframe> |
| 132 | + " |
| 133 | + `); |
| 134 | +}); |
0 commit comments