|
| 1 | +import { visit } from 'unist-util-visit' |
| 2 | +import { RESOURCES_BASE_URL } from 'uiSrc/services/resourcesService' |
| 3 | +import { remarkImage } from '../transform/remarkImage' |
| 4 | + |
| 5 | +jest.mock('unist-util-visit') |
| 6 | +const TUTORIAL_PATH = 'static/custom-tutorials/tutorial-id' |
| 7 | +const testCases = [ |
| 8 | + { |
| 9 | + url: '../../../_images/relative.png', |
| 10 | + path: `${TUTORIAL_PATH}/lvl1/lvl2/lvl3/intro.md`, |
| 11 | + result: `${RESOURCES_BASE_URL}${TUTORIAL_PATH}/_images/relative.png`, |
| 12 | + }, |
| 13 | + { |
| 14 | + url: '/../../../_images/relative.png', // NOTE: will not work in real. There is no sense to even support absolute paths |
| 15 | + path: `${TUTORIAL_PATH}/lvl1/lvl2/lvl3/intro.md`, |
| 16 | + result: `${RESOURCES_BASE_URL}_images/relative.png`, |
| 17 | + }, |
| 18 | + { |
| 19 | + url: 'https://somesite.test/image.png', |
| 20 | + path: `${TUTORIAL_PATH}/lvl1/lvl2/lvl3/intro.md`, |
| 21 | + result: 'https://somesite.test/image.png', |
| 22 | + } |
| 23 | +] |
| 24 | +describe('remarkImage', () => { |
| 25 | + testCases.forEach((tc) => { |
| 26 | + it(`should return ${tc.result} for url:${tc.url}, path: ${tc.path} `, () => { |
| 27 | + const node = { |
| 28 | + url: tc.url, |
| 29 | + }; |
| 30 | + |
| 31 | + // mock implementation |
| 32 | + (visit as jest.Mock) |
| 33 | + .mockImplementation((_tree: any, _name: string, callback: (node: any) => void) => { callback(node) }) |
| 34 | + |
| 35 | + const remark = remarkImage(tc.path) |
| 36 | + remark({} as Node) |
| 37 | + expect(node).toEqual({ |
| 38 | + ...node, |
| 39 | + url: tc.result, |
| 40 | + }) |
| 41 | + }) |
| 42 | + }) |
| 43 | +}) |
0 commit comments