|
| 1 | +import cases from 'jest-in-case'; |
| 2 | +import fetchMock from 'node-fetch'; |
| 3 | + |
| 4 | +import plugin from '../../'; |
| 5 | +import { |
| 6 | + getHTML, |
| 7 | + getNormalizedStreamableUrl, |
| 8 | + shouldTransform, |
| 9 | +} from '../../transformers/Streamable'; |
| 10 | + |
| 11 | +import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers'; |
| 12 | + |
| 13 | +jest.mock('node-fetch', () => jest.fn()); |
| 14 | + |
| 15 | +const mockFetch = html => |
| 16 | + fetchMock.mockResolvedValue({ json: () => Promise.resolve({ html }) }); |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + fetchMock.mockClear(); |
| 20 | +}); |
| 21 | + |
| 22 | +cases( |
| 23 | + 'url validation', |
| 24 | + ({ url, valid }) => { |
| 25 | + expect(shouldTransform(url)).toBe(valid); |
| 26 | + }, |
| 27 | + { |
| 28 | + 'non-Streamable url': { |
| 29 | + url: 'https://not-a-streamable-url.com', |
| 30 | + valid: false, |
| 31 | + }, |
| 32 | + "non-Streamable url ending with 'streamable.com'": { |
| 33 | + url: 'https://this-is-not-streamable.com', |
| 34 | + valid: false, |
| 35 | + }, |
| 36 | + 'documentation page': { |
| 37 | + url: 'https://streamable.com/documentation', |
| 38 | + valid: false, |
| 39 | + }, |
| 40 | + 'login page': { |
| 41 | + url: 'https://streamable.com/login', |
| 42 | + valid: false, |
| 43 | + }, |
| 44 | + 'recover page': { |
| 45 | + url: 'https://streamable.com/recover', |
| 46 | + valid: false, |
| 47 | + }, |
| 48 | + 'settings page': { |
| 49 | + url: 'https://streamable.com/settings', |
| 50 | + valid: false, |
| 51 | + }, |
| 52 | + 'signup page': { |
| 53 | + url: 'https://streamable.com/signup', |
| 54 | + valid: false, |
| 55 | + }, |
| 56 | + "video url having '/e/' path & username & extra path": { |
| 57 | + url: 'https://streamable.com/e/moo/username/extra', |
| 58 | + valid: false, |
| 59 | + }, |
| 60 | + "video url having '/g/' path & username & extra path": { |
| 61 | + url: 'https://streamable.com/g/moo/username/extra', |
| 62 | + valid: false, |
| 63 | + }, |
| 64 | + "video url having '/o/' path & username & extra path": { |
| 65 | + url: 'https://streamable.com/o/moo/username/extra', |
| 66 | + valid: false, |
| 67 | + }, |
| 68 | + "video url having '/s/' path & username & extra path": { |
| 69 | + url: 'https://streamable.com/s/moo/username/extra', |
| 70 | + valid: false, |
| 71 | + }, |
| 72 | + "video url having '/t/' path & username & extra path": { |
| 73 | + url: 'https://streamable.com/t/moo/username/extra', |
| 74 | + valid: false, |
| 75 | + }, |
| 76 | + 'video url': { |
| 77 | + url: 'https://streamable.com/moo', |
| 78 | + valid: true, |
| 79 | + }, |
| 80 | + "video url having '/e/' path": { |
| 81 | + url: 'https://streamable.com/e/moo', |
| 82 | + valid: true, |
| 83 | + }, |
| 84 | + "video url having '/e/' path & username": { |
| 85 | + url: 'https://streamable.com/e/moo/username', |
| 86 | + valid: true, |
| 87 | + }, |
| 88 | + "video url having '/g/' path": { |
| 89 | + url: 'https://streamable.com/g/moo', |
| 90 | + valid: true, |
| 91 | + }, |
| 92 | + "video url having '/g/' path & username": { |
| 93 | + url: 'https://streamable.com/g/moo/username', |
| 94 | + valid: true, |
| 95 | + }, |
| 96 | + "video url having '/o/' path": { |
| 97 | + url: 'https://streamable.com/o/moo', |
| 98 | + valid: true, |
| 99 | + }, |
| 100 | + "video url having '/o/' path & username": { |
| 101 | + url: 'https://streamable.com/o/moo/username', |
| 102 | + valid: true, |
| 103 | + }, |
| 104 | + "video url having '/s/' path": { |
| 105 | + url: 'https://streamable.com/s/moo', |
| 106 | + valid: true, |
| 107 | + }, |
| 108 | + "video url having '/s/' path & username": { |
| 109 | + url: 'https://streamable.com/s/moo/username', |
| 110 | + valid: true, |
| 111 | + }, |
| 112 | + "video url having '/t/' path": { |
| 113 | + url: 'https://streamable.com/t/moo', |
| 114 | + valid: true, |
| 115 | + }, |
| 116 | + "video url having '/t/' path & username": { |
| 117 | + url: 'https://streamable.com/t/moo/username', |
| 118 | + valid: true, |
| 119 | + }, |
| 120 | + } |
| 121 | +); |
| 122 | + |
| 123 | +cases( |
| 124 | + 'getNormalizedStreamableUrl', |
| 125 | + ({ url, normalizedUrl }) => { |
| 126 | + expect(getNormalizedStreamableUrl(url)).toBe(normalizedUrl); |
| 127 | + }, |
| 128 | + { |
| 129 | + 'video url': { |
| 130 | + url: 'https://streamable.com/moo', |
| 131 | + normalizedUrl: 'https://streamable.com/moo', |
| 132 | + }, |
| 133 | + "video url having '/e/' path": { |
| 134 | + url: 'https://streamable.com/e/moo', |
| 135 | + normalizedUrl: 'https://streamable.com/moo', |
| 136 | + }, |
| 137 | + "video url having '/e/' path & username": { |
| 138 | + url: 'https://streamable.com/e/moo/username', |
| 139 | + normalizedUrl: 'https://streamable.com/moo', |
| 140 | + }, |
| 141 | + "video url having '/g/' path": { |
| 142 | + url: 'https://streamable.com/g/moo', |
| 143 | + normalizedUrl: 'https://streamable.com/moo', |
| 144 | + }, |
| 145 | + "video url having '/g/' path & username": { |
| 146 | + url: 'https://streamable.com/g/moo/username', |
| 147 | + normalizedUrl: 'https://streamable.com/moo', |
| 148 | + }, |
| 149 | + "video url having '/o/' path": { |
| 150 | + url: 'https://streamable.com/o/moo', |
| 151 | + normalizedUrl: 'https://streamable.com/moo', |
| 152 | + }, |
| 153 | + "video url having '/o/' path & username": { |
| 154 | + url: 'https://streamable.com/o/moo/username', |
| 155 | + normalizedUrl: 'https://streamable.com/moo', |
| 156 | + }, |
| 157 | + "video url having '/s/' path": { |
| 158 | + url: 'https://streamable.com/s/moo', |
| 159 | + normalizedUrl: 'https://streamable.com/moo', |
| 160 | + }, |
| 161 | + "video url having '/s/' path & username": { |
| 162 | + url: 'https://streamable.com/s/moo/username', |
| 163 | + normalizedUrl: 'https://streamable.com/moo', |
| 164 | + }, |
| 165 | + "video url having '/t/' path": { |
| 166 | + url: 'https://streamable.com/t/moo', |
| 167 | + normalizedUrl: 'https://streamable.com/moo', |
| 168 | + }, |
| 169 | + "video url having '/t/' path & username": { |
| 170 | + url: 'https://streamable.com/t/moo/username', |
| 171 | + normalizedUrl: 'https://streamable.com/moo', |
| 172 | + }, |
| 173 | + } |
| 174 | +); |
| 175 | + |
| 176 | +test('Gets the correct Streamable iframe', async () => { |
| 177 | + mockFetch( |
| 178 | + `<iframe class="streamable-embed-mocked-fetch-transformer" src="https://streamable.com/o/moo" frameborder="0" scrolling="no" width="1920" height="1080" allowfullscreen></iframe>` |
| 179 | + ); |
| 180 | + const html = await getHTML('https://streamable.com/moo'); |
| 181 | + |
| 182 | + expect(html).toMatchInlineSnapshot( |
| 183 | + `"<iframe class=\\"streamable-embed-mocked-fetch-transformer\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe>"` |
| 184 | + ); |
| 185 | +}); |
| 186 | + |
| 187 | +test('Plugin correctly transforms Streamable links', async () => { |
| 188 | + mockFetch( |
| 189 | + `<iframe class="streamable-embed-mocked-fetch-plugin" src="https://streamable.com/o/moo" frameborder="0" scrolling="no" width="1920" height="1080" allowfullscreen></iframe>` |
| 190 | + ); |
| 191 | + const markdownAST = getMarkdownASTForFile('Streamable'); |
| 192 | + |
| 193 | + const processedAST = await plugin({ cache, markdownAST }); |
| 194 | + |
| 195 | + expect(parseASTToMarkdown(processedAST)).toMatchInlineSnapshot(` |
| 196 | + "<https://not-a-streamable-url.com> |
| 197 | +
|
| 198 | + <https://this-is-not-streamable.com> |
| 199 | +
|
| 200 | + <https://streamable.com/documentation> |
| 201 | +
|
| 202 | + <https://streamable.com/login> |
| 203 | +
|
| 204 | + <https://streamable.com/recover> |
| 205 | +
|
| 206 | + <https://streamable.com/settings> |
| 207 | +
|
| 208 | + <https://streamable.com/signup> |
| 209 | +
|
| 210 | + <https://streamable.com/e/moo/username/extra> |
| 211 | +
|
| 212 | + <https://streamable.com/g/moo/username/extra> |
| 213 | +
|
| 214 | + <https://streamable.com/o/moo/username/extra> |
| 215 | +
|
| 216 | + <https://streamable.com/s/moo/username/extra> |
| 217 | +
|
| 218 | + <https://streamable.com/t/moo/username/extra> |
| 219 | +
|
| 220 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 221 | +
|
| 222 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 223 | +
|
| 224 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 225 | +
|
| 226 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 227 | +
|
| 228 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 229 | +
|
| 230 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 231 | +
|
| 232 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 233 | +
|
| 234 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 235 | +
|
| 236 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 237 | +
|
| 238 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 239 | +
|
| 240 | + <iframe class=\\"streamable-embed-mocked-fetch-plugin\\" src=\\"https://streamable.com/o/moo\\" frameborder=\\"0\\" scrolling=\\"no\\" width=\\"1920\\" height=\\"1080\\" allowfullscreen></iframe> |
| 241 | + " |
| 242 | + `); |
| 243 | +}); |
0 commit comments