|
| 1 | +import Vue from 'vue' |
| 2 | +import {render, fireEvent, screen, waitFor} from '@testing-library/vue' |
| 3 | + |
| 4 | +declare const elem: HTMLElement |
| 5 | + |
| 6 | +const SomeComponent = Vue.extend({ |
| 7 | + name: 'SomeComponent', |
| 8 | + props: { |
| 9 | + foo: {type: Number, default: 0}, |
| 10 | + bar: {type: String, default: '0'}, |
| 11 | + }, |
| 12 | +}) |
| 13 | + |
| 14 | +export async function testRender() { |
| 15 | + const page = render({template: '<div />'}) |
| 16 | + |
| 17 | + // single queries |
| 18 | + page.getByText('foo') |
| 19 | + page.queryByText('foo') |
| 20 | + await page.findByText('foo') |
| 21 | + |
| 22 | + // multiple queries |
| 23 | + page.getAllByText('bar') |
| 24 | + page.queryAllByText('bar') |
| 25 | + await page.findAllByText('bar') |
| 26 | + |
| 27 | + // helpers |
| 28 | + const {container, unmount, debug} = page |
| 29 | + |
| 30 | + debug(container) |
| 31 | + |
| 32 | + debug(elem) // $ExpectType void |
| 33 | + debug([elem, elem], 100, {highlight: false}) // $ExpectType void |
| 34 | + |
| 35 | + unmount() |
| 36 | +} |
| 37 | + |
| 38 | +export function testRenderOptions() { |
| 39 | + const container = document.createElement('div') |
| 40 | + const options = {container} |
| 41 | + render({template: 'div'}, options) |
| 42 | +} |
| 43 | + |
| 44 | +export async function testFireEvent() { |
| 45 | + const {container} = render({template: 'button'}) |
| 46 | + await fireEvent.click(container) |
| 47 | +} |
| 48 | + |
| 49 | +export function testDebug() { |
| 50 | + const {debug, getAllByTestId} = render({ |
| 51 | + render(h) { |
| 52 | + return h('div', [ |
| 53 | + h('h1', {attrs: {'data-testId': 'testid'}}, 'hello world'), |
| 54 | + h('h2', {attrs: {'data-testId': 'testid'}}, 'hello world'), |
| 55 | + ]) |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + debug(getAllByTestId('testid')) |
| 60 | +} |
| 61 | + |
| 62 | +export async function testScreen() { |
| 63 | + render({template: 'button'}) |
| 64 | + |
| 65 | + await screen.findByRole('button') |
| 66 | +} |
| 67 | + |
| 68 | +export async function testWaitFor() { |
| 69 | + const {container} = render({template: 'button'}) |
| 70 | + await fireEvent.click(container) |
| 71 | + await waitFor(() => {}) |
| 72 | +} |
| 73 | + |
| 74 | +export function testOptions() { |
| 75 | + render(SomeComponent, { |
| 76 | + // options for new Vue() |
| 77 | + name: 'SomeComponent', |
| 78 | + methods: { |
| 79 | + glorb() { |
| 80 | + return 42 |
| 81 | + }, |
| 82 | + }, |
| 83 | + // options for vue-test-utils mount() |
| 84 | + slots: { |
| 85 | + quux: '<p>Baz</p>', |
| 86 | + }, |
| 87 | + mocks: { |
| 88 | + isThisFake() { |
| 89 | + return true |
| 90 | + }, |
| 91 | + }, |
| 92 | + // options for Vue Testing Library render() |
| 93 | + container: elem, |
| 94 | + baseElement: elem, |
| 95 | + props: { |
| 96 | + foo: 9, |
| 97 | + bar: 'x', |
| 98 | + }, |
| 99 | + store: { |
| 100 | + state: { |
| 101 | + foos: [4, 5], |
| 102 | + bars: ['a', 'b'], |
| 103 | + }, |
| 104 | + getters: { |
| 105 | + fooCount() { |
| 106 | + return this.foos.length |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + routes: [ |
| 111 | + {path: '/', name: 'home', component: SomeComponent}, |
| 112 | + { |
| 113 | + path: '/about', |
| 114 | + name: 'about', |
| 115 | + component: () => Promise.resolve(SomeComponent), |
| 116 | + }, |
| 117 | + ], |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +export function testConfigCallback() { |
| 122 | + const ExamplePlugin: Vue.PluginFunction<never> = () => {} |
| 123 | + render(SomeComponent, {}, (localVue, store, router) => { |
| 124 | + localVue.use(ExamplePlugin) |
| 125 | + store.replaceState({foo: 'bar'}) |
| 126 | + router.onError(error => console.log(error.message)) |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +/* |
| 131 | +eslint |
| 132 | + testing-library/prefer-explicit-assert: "off", |
| 133 | + testing-library/no-wait-for-empty-callback: "off", |
| 134 | + testing-library/no-debug: "off", |
| 135 | + testing-library/prefer-screen-queries: "off", |
| 136 | + @typescript-eslint/unbound-method: "off", |
| 137 | +*/ |
0 commit comments