|
| 1 | +const addStylesClient = require('../lib/addStylesClient') |
| 2 | +const addStylesServer = require('../lib/addStylesServer') |
| 3 | + |
| 4 | +const mockedList = [ |
| 5 | + [1, 'h1 { color: red; }', ''], |
| 6 | + [1, 'p { color: green; }', ''], |
| 7 | + [2, 'span { color: blue; }', ''], |
| 8 | + [2, 'span { color: blue; }', 'print'] |
| 9 | +] |
| 10 | + |
| 11 | +test('addStylesClient (dev)', () => { |
| 12 | + const update = addStylesClient('foo', mockedList, false) |
| 13 | + assertStylesMatch(mockedList) |
| 14 | + const mockedList2 = mockedList.slice(1, 3) |
| 15 | + update(mockedList2) |
| 16 | + assertStylesMatch(mockedList2) |
| 17 | + update() |
| 18 | + expect(document.querySelectorAll('style').length).toBe(0) |
| 19 | +}) |
| 20 | + |
| 21 | +test('addStylesClient (prod)', () => { |
| 22 | + const update = addStylesClient('foo', mockedList, true) |
| 23 | + assertStylesMatch(mockedList) |
| 24 | + const mockedList2 = mockedList.slice(2) |
| 25 | + update(mockedList2) |
| 26 | + assertStylesMatch(mockedList2) |
| 27 | + update() |
| 28 | + expect(document.querySelectorAll('style').length).toBe(0) |
| 29 | +}) |
| 30 | + |
| 31 | +test('addStylesClient (dev + ssr)', () => { |
| 32 | + mockSSRTags(mockedList, 'foo') |
| 33 | + const update = addStylesClient('foo', mockedList, false) |
| 34 | + assertStylesMatch(mockedList) |
| 35 | + update() |
| 36 | + expect(document.querySelectorAll('style').length).toBe(0) |
| 37 | +}) |
| 38 | + |
| 39 | +test('addStylesClient (prod + ssr)', () => { |
| 40 | + mockProdSSRTags(mockedList, 'foo') |
| 41 | + const update = addStylesClient('foo', mockedList, true) |
| 42 | + expect(document.querySelectorAll('style').length).toBe(1) |
| 43 | +}) |
| 44 | + |
| 45 | +test('addStylesServer (dev)', () => { |
| 46 | + const context = global.__VUE_SSR_CONTEXT__ = {} |
| 47 | + addStylesServer('foo', mockedList, false) |
| 48 | + expect(context.styles).toBe( |
| 49 | + `<style data-vue-ssr-id="foo:0">h1 { color: red; }</style>` + |
| 50 | + `<style data-vue-ssr-id="foo:1">p { color: green; }</style>` + |
| 51 | + `<style data-vue-ssr-id="foo:2">span { color: blue; }</style>` + |
| 52 | + `<style data-vue-ssr-id="foo:3" media="print">span { color: blue; }</style>` |
| 53 | + ) |
| 54 | +}) |
| 55 | + |
| 56 | +test('addStylesServer (prod)', () => { |
| 57 | + const context = global.__VUE_SSR_CONTEXT__ = {} |
| 58 | + addStylesServer('foo', mockedList, true) |
| 59 | + expect(context.styles).toBe( |
| 60 | + `<style data-vue-ssr-id="foo:0 foo:1 foo:2">` + |
| 61 | + `h1 { color: red; }\np { color: green; }\nspan { color: blue; }` + |
| 62 | + `</style>` + |
| 63 | + `<style data-vue-ssr-id="foo:3" media="print">span { color: blue; }</style>` |
| 64 | + ) |
| 65 | +}) |
| 66 | + |
| 67 | +// --- helpers --- |
| 68 | + |
| 69 | +function assertStylesMatch (list) { |
| 70 | + const styles = document.querySelectorAll('style') |
| 71 | + expect(styles.length).toBe(list.length) |
| 72 | + ;[].forEach.call(styles, (style, i) => { |
| 73 | + expect(style.textContent.indexOf(list[i][1]) > -1).toBe(true) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +function mockSSRTags (list, parentId) { |
| 78 | + list.forEach((item, i) => { |
| 79 | + const style = document.createElement('style') |
| 80 | + style.setAttribute('data-vue-ssr-id', `${parentId}:${i}`) |
| 81 | + style.textContent = item[1] |
| 82 | + if (item[2]) { |
| 83 | + style.setAttribute('media', item[2]) |
| 84 | + } |
| 85 | + document.head.appendChild(style) |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +function mockProdSSRTags (list, parentId) { |
| 90 | + const style = document.createElement('style') |
| 91 | + style.setAttribute('data-vue-ssr-id', list.map((item, i) => `${parentId}:${i}`).join(' ')) |
| 92 | + style.textContent = list.map(item => item[1]).join('\n') |
| 93 | + document.head.appendChild(style) |
| 94 | +} |
0 commit comments