|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 OneLiteFeather |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { expect } from 'vitest' |
| 18 | +import type { VueWrapper } from '@vue/test-utils' |
| 19 | +import MdTextField from '../components/md/MdTextField.vue' |
| 20 | +import MdRadio from '../components/md/MdRadio.vue' |
| 21 | +import MdCheckbox from '../components/md/MdCheckbox.vue' |
| 22 | + |
| 23 | +/* |
| 24 | + * The form rule of the dashboard, as an assertion for the test of a component: every text field is the |
| 25 | + * default `outlined` variant, and every radio button and checkbox on the screen is drawn by a primitive, |
| 26 | + * never by a hand-built native input. `root` is the element to look into; a dialog is teleported, so it |
| 27 | + * is `document.body` there. |
| 28 | + */ |
| 29 | +const expectPrimitiveControls = (wrapper: VueWrapper, scope?: ParentNode) => { |
| 30 | + const root: ParentNode = scope ?? (wrapper.element as Element) |
| 31 | + const variants = wrapper.findAllComponents(MdTextField).map(field => field.props('variant') as string) |
| 32 | + expect(variants.length, 'the component draws text fields').toBeGreaterThan(0) |
| 33 | + expect(variants.every(variant => variant === 'outlined'), `every text field is outlined, got ${variants.join(', ')}`).toBe(true) |
| 34 | + |
| 35 | + expect( |
| 36 | + root.querySelectorAll('input[type="radio"]').length, |
| 37 | + 'every radio button is an MdRadio' |
| 38 | + ).toBe(wrapper.findAllComponents(MdRadio).length) |
| 39 | + expect( |
| 40 | + root.querySelectorAll('input[type="checkbox"]').length, |
| 41 | + 'every checkbox is an MdCheckbox' |
| 42 | + ).toBe(wrapper.findAllComponents(MdCheckbox).length) |
| 43 | +} |
| 44 | + |
| 45 | +export { expectPrimitiveControls } |
0 commit comments