123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- import { haveAttribute, haveFocus, html, haveClasses, notHaveClasses, test, haveText, notExist, beHidden, } from '../../../utils'
- test('it works using x-model',
- [html`
- <main x-data="{ active: null, access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio x-model="active">
- <fieldset>
- <legend>
- <h2 x-radio:label>Privacy setting</h2>
- </legend>
- <div>
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div :option="id" x-radio:option :value="id" :disabled="disabled">
- <span x-radio:label x-text="name"></span>
- <span x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- </fieldset>
- </div>
- <input x-model="active" type="hidden">
- </main>
- `],
- ({ get }) => {
- get('input').should(haveAttribute('value', ''))
- get('[option="access-2"]').click()
- get('input').should(haveAttribute('value', 'access-2'))
- get('[option="access-4"]').click()
- get('input').should(haveAttribute('value', 'access-4'))
- },
- )
- test('it works without x-model/with default-value',
- [html`
- <main x-data="{ access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio default-value="access-4">
- <fieldset>
- <legend>
- <h2 x-radio:label>Privacy setting</h2>
- </legend>
- <div>
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div :option="id" x-radio:option :value="id" :disabled="disabled">
- <span x-radio:label x-text="name"></span>
- <span x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- </fieldset>
- </div>
- </main>
- `],
- ({ get }) => {
- get('[option="access-4"]').should(haveAttribute('aria-checked', 'true'))
- get('[option="access-2"]').click()
- get('[option="access-2"]').should(haveAttribute('aria-checked', 'true'))
- },
- )
- test('cannot select any option when the group is disabled',
- [html`
- <main x-data="{ active: null, access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio x-model="active" disabled>
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div :option="id" x-radio:option :value="id" :disabled="disabled">
- <span x-radio:label x-text="name"></span>
- <span x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- <input x-model="active" type="hidden">
- </main>
- `],
- ({ get }) => {
- get('input').should(haveAttribute('value', ''))
- get('[option="access-1"]').click()
- get('input').should(haveAttribute('value', ''))
- },
- )
- test('cannot select a disabled option',
- [html`
- <main x-data="{ active: null, access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio x-model="active">
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div :option="id" x-radio:option :value="id" :disabled="disabled">
- <span x-radio:label x-text="name"></span>
- <span x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- <input x-model="active" type="hidden">
- </main>
- `],
- ({ get }) => {
- get('input').should(haveAttribute('value', ''))
- get('[option="access-3"]').click()
- get('input').should(haveAttribute('value', ''))
- },
- )
- test('keyboard navigation works',
- [html`
- <main x-data="{ active: null, access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio x-model="active">
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div :option="id" x-radio:option :value="id" :disabled="disabled">
- <span x-radio:label x-text="name"></span>
- <span x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- <input x-model="active" type="hidden">
- </main>
- `],
- ({ get }) => {
- get('input').should(haveAttribute('value', ''))
- get('[option="access-1"]').focus().type('{downarrow}')
- get('[option="access-2"]').should(haveFocus()).type('{downarrow}')
- get('[option="access-4"]').should(haveFocus()).type('{downarrow}')
- get('[option="access-1"]').should(haveFocus())
- },
- )
- test('has accessibility attributes',
- [html`
- <main x-data="{ active: null, access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio group x-model="active">
- <fieldset>
- <legend>
- <h2 x-radio:label>Privacy setting</h2>
- <p x-radio:description>Some description</p>
- </legend>
- <div>
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div :option="id" x-radio:option :value="id" :disabled="disabled">
- <span :label="id" x-radio:label x-text="name"></span>
- <span :description="id" x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- </fieldset>
- </div>
- </main>
- `],
- ({ get }) => {
- get('[group]').should(haveAttribute('role', 'radiogroup'))
- .should(haveAttribute('aria-labelledby', 'alpine-radio-label-1'))
- .should(haveAttribute('aria-describedby', 'alpine-radio-description-1'))
- get('h2').should(haveAttribute('id', 'alpine-radio-label-1'))
- get('p').should(haveAttribute('id', 'alpine-radio-description-1'))
- get('[option="access-1"]')
- .should(haveAttribute('tabindex', 0))
- for (i in 4) {
- get(`[option="access-${i}"]`)
- .should(haveAttribute('role', 'radio'))
- .should(haveAttribute('aria-disabled', 'false'))
- .should(haveAttribute('aria-labelledby', `alpine-radio-label-${i + 1}`))
- .should(haveAttribute('aria-describedby', `alpine-radio-description-${i + 1}`))
- get(`[label="access-${i}"]`)
- .should(haveAttribute('id', `alpine-radio-label-${i + 1}`))
- get(`[description="access-${i}"]`)
- .should(haveAttribute('id', `alpine-radio-description-${i + 1}`))
- }
- get('[option="access-1"]')
- .click()
- .should(haveAttribute('aria-checked', 'true'))
- },
- )
- test('$radioOption.isActive, $radioOption.isChecked, $radioOption.isDisabled work',
- [html`
- <main x-data="{ access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio group>
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div
- :option="id"
- x-radio:option
- :value="id"
- :disabled="disabled"
- :class="{
- 'active': $radioOption.isActive,
- 'checked': $radioOption.isChecked,
- 'disabled': $radioOption.isDisabled,
- }"
- >
- <span :label="id" x-radio:label x-text="name"></span>
- <span :description="id" x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- </main>
- `],
- ({ get }) => {
- get('[option="access-1"]')
- .should(notHaveClasses(['active', 'checked', 'disabled']))
- .focus()
- .should(haveClasses(['active']))
- .should(notHaveClasses(['checked']))
- .type(' ')
- .should(haveClasses(['active', 'checked']))
- .type('{downarrow}')
- get('[option="access-2"]')
- .should(haveClasses(['active', 'checked']))
- get('[option="access-3"]')
- .should(haveClasses(['disabled']))
- },
- )
- test('can bind objects to the value',
- [html`
- <main x-data="{ active: null, access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]}">
- <div x-radio group x-model="active">
- <template x-for="(option, i) in access" :key="option.id">
- <div
- :option="option.id"
- x-radio:option
- :value="option"
- :disabled="option.disabled"
- >
- <span :label="option.id" x-radio:label x-text="option.name"></span>
- <span :description="option.id" x-radio:description x-text="option.description"></span>
- </div>
- </template>
- </div>
- <article x-text="JSON.stringify(active)"></article>
- </main>
- `],
- ({ get }) => {
- get('[option="access-2"]').click()
- get('article')
- .should(haveText(JSON.stringify({
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- })))
- },
- )
- test('name prop',
- [html`
- <main
- x-data="{
- active: null,
- access: [
- {
- id: 'access-1',
- name: 'Public access',
- description: 'This project would be available to anyone who has the link',
- disabled: false,
- },
- {
- id: 'access-2',
- name: 'Private to Project Members',
- description: 'Only members of this project would be able to access',
- disabled: false,
- },
- {
- id: 'access-3',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: true,
- },
- {
- id: 'access-4',
- name: 'Private to you',
- description: 'You are the only one able to access this project',
- disabled: false,
- },
- ]
- }
- ">
- <div x-radio group x-model="active" name="access">
- <template x-for="({ id, name, description, disabled }, i) in access" :key="id">
- <div
- :option="id"
- x-radio:option
- :value="id"
- :disabled="disabled"
- >
- <span :label="id" x-radio:label x-text="name"></span>
- <span :description="id" x-radio:description x-text="description"></span>
- </div>
- </template>
- </div>
- </main>
- `],
- ({ get }) => {
- get('input').should(notExist())
- get('[option="access-2"]').click()
- get('input').should(beHidden())
- .should(haveAttribute('name', 'access'))
- .should(haveAttribute('value', 'access-2'))
- .should(haveAttribute('type', 'hidden'))
- get('[option="access-4"]').click()
- get('input').should(beHidden())
- .should(haveAttribute('name', 'access'))
- .should(haveAttribute('value', 'access-4'))
- .should(haveAttribute('type', 'hidden'))
- },
- )
|