|
| 1 | +import { render as vRender } from '@vue/server-test-utils'; |
| 2 | +import { mount as vMount, shallowMount as vShallowMount, createLocalVue } from '@vue/test-utils'; |
| 3 | +import merge from 'lodash.merge'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Generate a local vue instance for testing environment. |
| 7 | + * |
| 8 | + * @param {Function} callback |
| 9 | + * |
| 10 | + * @returns {VueConstructor<Vue>} |
| 11 | + */ |
| 12 | +const makeLocalVue = (callback = undefined) => { |
| 13 | + const localVue = createLocalVue(); |
| 14 | + |
| 15 | + if (callback && typeof callback === 'function') { |
| 16 | + callback(localVue); |
| 17 | + } |
| 18 | + |
| 19 | + return localVue; |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * Generate a vue component from a story. |
| 24 | + * |
| 25 | + * @param {Function} story |
| 26 | + * |
| 27 | + * @returns {object} |
| 28 | + */ |
| 29 | +export const generateStory = (story) => story(story.args, { |
| 30 | + argTypes: story.args, |
| 31 | +}); |
| 32 | + |
| 33 | +/** |
| 34 | + * Generate the config for the mounting methods. |
| 35 | + * |
| 36 | + * @param {object} config |
| 37 | + * |
| 38 | + * @returns {object} |
| 39 | + */ |
| 40 | +const generateConfig = (config) => merge({ |
| 41 | + stubs: { |
| 42 | + icon: true, |
| 43 | + 'nuxt-link': true, |
| 44 | + }, |
| 45 | + localVue: makeLocalVue(), |
| 46 | +}, config); |
| 47 | + |
| 48 | +/** |
| 49 | + * Generate the config for the story mounting methods. |
| 50 | + * |
| 51 | + * @param {object} story |
| 52 | + * @param {object} options |
| 53 | + * |
| 54 | + * @returns {object} |
| 55 | + */ |
| 56 | +const generateConfigForStory = (story, options) => merge(generateConfig(options), { |
| 57 | + propsData: { |
| 58 | + ...story.args, |
| 59 | + ...options.propsData, |
| 60 | + }, |
| 61 | + localVue: makeLocalVue(options.localVue), |
| 62 | +}); |
| 63 | + |
| 64 | +/** |
| 65 | + * Render a story using `@vue/server-test-utils`. |
| 66 | + * |
| 67 | + * @param {Function} story |
| 68 | + * @param {object} config |
| 69 | + * |
| 70 | + * @returns {Promise<Cheerio>} |
| 71 | + */ |
| 72 | +export const renderStory = (story, config = {}) => { |
| 73 | + return vRender(generateStory(story), generateConfigForStory(story, config)); |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Mount a story using `@vue/test-utils`. |
| 78 | + * |
| 79 | + * @param {Function} story |
| 80 | + * @param {object} config |
| 81 | + * |
| 82 | + * @returns {Promise<Cheerio>} |
| 83 | + */ |
| 84 | +export const mountStory = (story, config = {}) => { |
| 85 | + return vMount(generateStory(story), generateConfigForStory(story, config)); |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * Shallow mount a story using `@vue/test-utils`. |
| 90 | + * |
| 91 | + * @param {Function} story |
| 92 | + * @param {object} config |
| 93 | + * @param {object} config.propsData |
| 94 | + * @param {object} config.options |
| 95 | + * |
| 96 | + * @returns {Promise<Cheerio>} |
| 97 | + */ |
| 98 | +export const shallowMountStory = (story, config = {}) => { |
| 99 | + return vShallowMount(generateStory(story), generateConfigForStory(story, config)); |
| 100 | +}; |
| 101 | + |
| 102 | +/** |
| 103 | + * Render a component using `@vue/server-test-utils`. |
| 104 | + * |
| 105 | + * @param {object} component |
| 106 | + * @param {object} config |
| 107 | + * |
| 108 | + * @returns {Promise<Cheerio>} |
| 109 | + */ |
| 110 | +export const render = (component, config = {}) => { |
| 111 | + return vRender(component, generateConfig(config)); |
| 112 | +}; |
| 113 | + |
| 114 | +/** |
| 115 | + * Mount a component using `@vue/test-utils`. |
| 116 | + * |
| 117 | + * @param {object} component |
| 118 | + * @param {object} config |
| 119 | + * |
| 120 | + * @returns {Promise<Cheerio>} |
| 121 | + */ |
| 122 | +export const mount = (component, config = {}) => { |
| 123 | + return vMount(component, generateConfig(config)); |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Shallow mount a component using `@vue/test-utils`. |
| 128 | + * |
| 129 | + * @param {object} component |
| 130 | + * @param {object} config |
| 131 | + * |
| 132 | + * @returns {Promise<Cheerio>} |
| 133 | + */ |
| 134 | +export const shallowMount = (component, config = {}) => { |
| 135 | + return vShallowMount(component, generateConfig(config)); |
| 136 | +}; |
0 commit comments