|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +import {ReactiveElement, PropertyValues} from '@lit/reactive-element'; |
| 8 | +import {state} from '@lit/reactive-element/decorators/state.js'; |
| 9 | +import {generateElementName} from '../../test-helpers.js'; |
| 10 | +import {assert} from 'chai'; |
| 11 | + |
| 12 | +suite('@state', () => { |
| 13 | + let container: HTMLElement; |
| 14 | + let el: E; |
| 15 | + |
| 16 | + const hasChanged = (value: any, old: any) => old === undefined || value > old; |
| 17 | + |
| 18 | + class E extends ReactiveElement { |
| 19 | + @state() |
| 20 | + accessor #prop: string | undefined = undefined; |
| 21 | + |
| 22 | + get prop() { |
| 23 | + return this.#prop; |
| 24 | + } |
| 25 | + |
| 26 | + set prop(v) { |
| 27 | + this.#prop = v; |
| 28 | + } |
| 29 | + |
| 30 | + @state({hasChanged}) |
| 31 | + accessor #hasChangedProp = 10; |
| 32 | + |
| 33 | + get hasChangedProp() { |
| 34 | + return this.#hasChangedProp; |
| 35 | + } |
| 36 | + |
| 37 | + set hasChangedProp(v) { |
| 38 | + this.#hasChangedProp = v; |
| 39 | + } |
| 40 | + |
| 41 | + #setterValue: string | undefined = undefined; |
| 42 | + |
| 43 | + @state() |
| 44 | + set #setter(v: string | undefined) { |
| 45 | + this.#setterValue = v; |
| 46 | + } |
| 47 | + get #setter() { |
| 48 | + return this.#setterValue; |
| 49 | + } |
| 50 | + |
| 51 | + set setter(v: string | undefined) { |
| 52 | + this.#setter = v; |
| 53 | + } |
| 54 | + get setter() { |
| 55 | + return this.#setter; |
| 56 | + } |
| 57 | + |
| 58 | + updateCount = 0; |
| 59 | + |
| 60 | + override update(changed: PropertyValues) { |
| 61 | + this.updateCount++; |
| 62 | + super.update(changed); |
| 63 | + } |
| 64 | + } |
| 65 | + customElements.define(generateElementName(), E); |
| 66 | + |
| 67 | + setup(async () => { |
| 68 | + container = document.createElement('div'); |
| 69 | + container.id = 'test-container'; |
| 70 | + document.body.appendChild(container); |
| 71 | + el = new E(); |
| 72 | + container.appendChild(el); |
| 73 | + await el.updateComplete; |
| 74 | + }); |
| 75 | + |
| 76 | + teardown(() => { |
| 77 | + if (container !== undefined) { |
| 78 | + container.parentElement!.removeChild(container); |
| 79 | + (container as any) = undefined; |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + test('triggers accessor update', async () => { |
| 84 | + assert.equal(el.updateCount, 1); |
| 85 | + el.prop = 'change'; |
| 86 | + await el.updateComplete; |
| 87 | + // If we don't read the new value of private accessors correctly, this will |
| 88 | + // fail. |
| 89 | + assert.equal(el.updateCount, 2); |
| 90 | + }); |
| 91 | + |
| 92 | + test('uses hasChanged', async () => { |
| 93 | + assert.equal(el.updateCount, 1); |
| 94 | + el.hasChangedProp = 100; |
| 95 | + await el.updateComplete; |
| 96 | + // If we don't read the new value of private accessors correctly, this will |
| 97 | + // fail. |
| 98 | + assert.equal(el.updateCount, 2); |
| 99 | + el.hasChangedProp = 0; |
| 100 | + await el.updateComplete; |
| 101 | + assert.equal(el.updateCount, 2); |
| 102 | + }); |
| 103 | + |
| 104 | + test('triggers setter update', async () => { |
| 105 | + assert.equal(el.updateCount, 1); |
| 106 | + el.setter = 'change'; |
| 107 | + await el.updateComplete; |
| 108 | + // If we don't read the new value of private accessors correctly, this will |
| 109 | + // fail. |
| 110 | + assert.equal(el.updateCount, 2); |
| 111 | + }); |
| 112 | +}); |
0 commit comments