|
| 1 | +import cases from 'jest-in-case'; |
| 2 | +import fetchMock from 'node-fetch'; |
| 3 | + |
| 4 | +import plugin from '../../'; |
| 5 | +import { |
| 6 | + getGIPHYId, |
| 7 | + getGIPHYResponsivePadding, |
| 8 | + getHTML, |
| 9 | + shouldTransform, |
| 10 | +} from '../../transformers/GIPHY'; |
| 11 | + |
| 12 | +import { cache, getMarkdownASTForFile, parseASTToMarkdown } from '../helpers'; |
| 13 | + |
| 14 | +jest.mock('node-fetch', () => jest.fn()); |
| 15 | + |
| 16 | +const mockFetch = ({ height, width }) => |
| 17 | + fetchMock.mockResolvedValue({ |
| 18 | + json: () => Promise.resolve({ height, width }), |
| 19 | + }); |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + fetchMock.mockClear(); |
| 23 | +}); |
| 24 | + |
| 25 | +cases( |
| 26 | + 'url validation', |
| 27 | + ({ url, valid }) => { |
| 28 | + expect(shouldTransform(url)).toBe(valid); |
| 29 | + }, |
| 30 | + { |
| 31 | + 'non-GIPHY url': { |
| 32 | + url: 'https://not-a-giphy-url.com', |
| 33 | + valid: false, |
| 34 | + }, |
| 35 | + "non-GIPHY url ending with 'giphy.com'": { |
| 36 | + url: 'https://this-is-not-giphy.com', |
| 37 | + valid: false, |
| 38 | + }, |
| 39 | + "non-GIPHY url ending with 'giphy.com' and having '/gifs/' in the url": { |
| 40 | + url: |
| 41 | + 'https://this-is-not-giphy.com/gifs/howtogiphygifs-how-to-XatG8bioEwwVO', |
| 42 | + valid: false, |
| 43 | + }, |
| 44 | + homepage: { |
| 45 | + url: 'https://giphy.com', |
| 46 | + valid: false, |
| 47 | + }, |
| 48 | + 'video url': { |
| 49 | + url: |
| 50 | + 'https://giphy.com/videos/blesstheharts-wayne-bless-the-harts-ciwJyqlgAYkvguS2Nw', |
| 51 | + valid: false, |
| 52 | + }, |
| 53 | + 'gif url': { |
| 54 | + url: 'https://giphy.com/gifs/howtogiphygifs-how-to-XatG8bioEwwVO', |
| 55 | + valid: true, |
| 56 | + }, |
| 57 | + "gif url having 'media' subdomain": { |
| 58 | + url: 'https://media.giphy.com/media/XatG8bioEwwVO/giphy.gif', |
| 59 | + valid: true, |
| 60 | + }, |
| 61 | + "gif url having 'media' subdomain and not ending on 'giphy.gif'": { |
| 62 | + url: 'https://media.giphy.com/media/XatG8bioEwwVO', |
| 63 | + valid: true, |
| 64 | + }, |
| 65 | + "gif url having 'media0' subdomain": { |
| 66 | + url: 'https://media0.giphy.com/media/XatG8bioEwwVO/giphy.gif', |
| 67 | + valid: true, |
| 68 | + }, |
| 69 | + "gif url having 'media0' subdomain and not ending on 'giphy.gif'": { |
| 70 | + url: 'https://media0.giphy.com/media/XatG8bioEwwVO', |
| 71 | + valid: true, |
| 72 | + }, |
| 73 | + } |
| 74 | +); |
| 75 | + |
| 76 | +cases( |
| 77 | + 'getGIPHYId', |
| 78 | + ({ id, url }) => { |
| 79 | + expect(getGIPHYId(url)).toBe(id); |
| 80 | + }, |
| 81 | + { |
| 82 | + 'media subdomain': { |
| 83 | + url: 'https://media.giphy.com/media/UUi1SJNYpMguAcnKsh/giphy.gif', |
| 84 | + id: 'UUi1SJNYpMguAcnKsh', |
| 85 | + }, |
| 86 | + 'media numbered subdomain': { |
| 87 | + url: 'https://media0.giphy.com/media/8P7qnlQ6o0NF5R8IEB/giphy.gif', |
| 88 | + id: '8P7qnlQ6o0NF5R8IEB', |
| 89 | + }, |
| 90 | + 'gif url': { |
| 91 | + url: 'https://giphy.com/gifs/howtogiphygifs-how-to-XatG8bioEwwVO', |
| 92 | + id: 'XatG8bioEwwVO', |
| 93 | + }, |
| 94 | + } |
| 95 | +); |
| 96 | + |
| 97 | +cases( |
| 98 | + 'getGIPHYResponsivePadding', |
| 99 | + ({ height, width, padding }) => { |
| 100 | + expect(getGIPHYResponsivePadding({ height, width })).toBe(padding); |
| 101 | + }, |
| 102 | + [ |
| 103 | + { |
| 104 | + height: 314, |
| 105 | + width: 500, |
| 106 | + padding: 63, |
| 107 | + }, |
| 108 | + { |
| 109 | + height: 270, |
| 110 | + width: 480, |
| 111 | + padding: 56, |
| 112 | + }, |
| 113 | + { |
| 114 | + height: 375, |
| 115 | + width: 500, |
| 116 | + padding: 75, |
| 117 | + }, |
| 118 | + { |
| 119 | + height: 151, |
| 120 | + width: 300, |
| 121 | + padding: 50, |
| 122 | + }, |
| 123 | + ] |
| 124 | +); |
| 125 | + |
| 126 | +test('Gets the correct GIPHY iframe', async () => { |
| 127 | + mockFetch({ height: 314, width: 500 }); |
| 128 | + |
| 129 | + const html = await getHTML( |
| 130 | + 'https://giphy.com/gifs/howtogiphygifs-how-to-XatG8bioEwwVO' |
| 131 | + ); |
| 132 | + |
| 133 | + expect(html).toMatchInlineSnapshot( |
| 134 | + `"<div style=\\"width:100%;height:0;padding-bottom:63%;position:relative;\\"><iframe src=\\"https://giphy.com/embed/XatG8bioEwwVO\\" width=\\"100%\\" height=\\"100%\\" style=\\"position:absolute\\" frameborder=\\"0\\" class=\\"giphy-embed\\" allowfullscreen></iframe></div>"` |
| 135 | + ); |
| 136 | +}); |
| 137 | + |
| 138 | +test('Plugin can transform GIPHY links', async () => { |
| 139 | + mockFetch({ height: 314, width: 500 }); |
| 140 | + |
| 141 | + const markdownAST = getMarkdownASTForFile('GIPHY'); |
| 142 | + |
| 143 | + const processedAST = await plugin({ cache, markdownAST }); |
| 144 | + |
| 145 | + expect(parseASTToMarkdown(processedAST)).toMatchInlineSnapshot(` |
| 146 | + "<https://not-a-giphy-url.com> |
| 147 | +
|
| 148 | + <https://this-is-not-giphy.com> |
| 149 | +
|
| 150 | + <https://this-is-not-giphy.com/gifs/howtogiphygifs-how-to-XatG8bioEwwVO> |
| 151 | +
|
| 152 | + <https://giphy.com> |
| 153 | +
|
| 154 | + <https://giphy.com/videos/blesstheharts-wayne-bless-the-harts-ciwJyqlgAYkvguS2Nw> |
| 155 | +
|
| 156 | + <div style=\\"width:100%;height:0;padding-bottom:63%;position:relative;\\"><iframe src=\\"https://giphy.com/embed/XatG8bioEwwVO\\" width=\\"100%\\" height=\\"100%\\" style=\\"position:absolute\\" frameborder=\\"0\\" class=\\"giphy-embed\\" allowfullscreen></iframe></div> |
| 157 | +
|
| 158 | + <div style=\\"width:100%;height:0;padding-bottom:63%;position:relative;\\"><iframe src=\\"https://giphy.com/embed/XatG8bioEwwVO\\" width=\\"100%\\" height=\\"100%\\" style=\\"position:absolute\\" frameborder=\\"0\\" class=\\"giphy-embed\\" allowfullscreen></iframe></div> |
| 159 | +
|
| 160 | + <div style=\\"width:100%;height:0;padding-bottom:63%;position:relative;\\"><iframe src=\\"https://giphy.com/embed/XatG8bioEwwVO\\" width=\\"100%\\" height=\\"100%\\" style=\\"position:absolute\\" frameborder=\\"0\\" class=\\"giphy-embed\\" allowfullscreen></iframe></div> |
| 161 | +
|
| 162 | + <div style=\\"width:100%;height:0;padding-bottom:63%;position:relative;\\"><iframe src=\\"https://giphy.com/embed/XatG8bioEwwVO\\" width=\\"100%\\" height=\\"100%\\" style=\\"position:absolute\\" frameborder=\\"0\\" class=\\"giphy-embed\\" allowfullscreen></iframe></div> |
| 163 | +
|
| 164 | + <div style=\\"width:100%;height:0;padding-bottom:63%;position:relative;\\"><iframe src=\\"https://giphy.com/embed/XatG8bioEwwVO\\" width=\\"100%\\" height=\\"100%\\" style=\\"position:absolute\\" frameborder=\\"0\\" class=\\"giphy-embed\\" allowfullscreen></iframe></div> |
| 165 | + " |
| 166 | + `); |
| 167 | +}); |
0 commit comments