|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// import 'jasmine'; (google3-only) |
| 8 | + |
| 9 | +import {html} from 'lit'; |
| 10 | +import {customElement} from 'lit/decorators.js'; |
| 11 | + |
| 12 | +import {Environment} from '../../testing/environment.js'; |
| 13 | +import {ButtonHarness} from '../harness.js'; |
| 14 | + |
| 15 | +import {Button} from './button.js'; |
| 16 | + |
| 17 | +@customElement('test-button') |
| 18 | +class TestButton extends Button {} |
| 19 | + |
| 20 | +describe('Button', () => { |
| 21 | + const env = new Environment(); |
| 22 | + |
| 23 | + async function setupTest() { |
| 24 | + const button = new TestButton(); |
| 25 | + env.render(html`${button}`); |
| 26 | + await env.waitForStability(); |
| 27 | + return {button, harness: new ButtonHarness(button)}; |
| 28 | + } |
| 29 | + |
| 30 | + it('should not be focusable when disabled', async () => { |
| 31 | + const {button} = await setupTest(); |
| 32 | + button.disabled = true; |
| 33 | + await env.waitForStability(); |
| 34 | + |
| 35 | + button.focus(); |
| 36 | + expect(document.activeElement).toEqual(document.body); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should be focusable when disabled and alwaysFocusable', async () => { |
| 40 | + const {button} = await setupTest(); |
| 41 | + button.disabled = true; |
| 42 | + button.alwaysFocusable = true; |
| 43 | + await env.waitForStability(); |
| 44 | + |
| 45 | + button.focus(); |
| 46 | + expect(document.activeElement).toEqual(button); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not activate if clicked when disabled', async () => { |
| 50 | + const clickListener = jasmine.createSpy('clickListener'); |
| 51 | + const {button} = await setupTest(); |
| 52 | + button.disabled = true; |
| 53 | + button.addEventListener('click', clickListener); |
| 54 | + await env.waitForStability(); |
| 55 | + |
| 56 | + button.click(); |
| 57 | + expect(clickListener).not.toHaveBeenCalled(); |
| 58 | + }); |
| 59 | +}); |
0 commit comments