|
| 1 | +import { setup } from '../../../setup.mjs'; |
| 2 | + |
| 3 | +const appName = 'AfterSetValueSequence'; |
| 4 | + |
| 5 | +setup({ |
| 6 | + appConfig: { |
| 7 | + name: appName |
| 8 | + } |
| 9 | +}); |
| 10 | + |
| 11 | +import { test, expect } from '@playwright/test'; |
| 12 | + |
| 13 | +import Neo from '../../../../../src/Neo.mjs'; |
| 14 | +import * as core from '../../../../../src/core/_export.mjs'; |
| 15 | +import InstanceManager from '../../../../../src/manager/Instance.mjs'; |
| 16 | +import ComboBox from '../../../../../src/form/field/ComboBox.mjs'; |
| 17 | +import Text from '../../../../../src/form/field/Text.mjs'; |
| 18 | + |
| 19 | +class MyComboBox extends ComboBox { |
| 20 | + static config = { |
| 21 | + className: 'Test.MyComboBox', |
| 22 | + |
| 23 | + displayField: 'country', |
| 24 | + valueField : 'country', |
| 25 | + value : 'Angola', // Initial value |
| 26 | + store: { |
| 27 | + keyProperty: 'country', |
| 28 | + model: { |
| 29 | + fields: [ |
| 30 | + {name: 'country', type: 'String'} |
| 31 | + ] |
| 32 | + }, |
| 33 | + data : [ |
| 34 | + {country: 'Angola'}, |
| 35 | + {country: 'Algeria'} |
| 36 | + ] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + afterSetValue(value, oldValue) { |
| 41 | + this.executionOrder.push('MyComboBox.afterSetValue'); |
| 42 | + super.afterSetValue(value, oldValue); |
| 43 | + } |
| 44 | +} |
| 45 | +MyComboBox = Neo.setupClass(MyComboBox); |
| 46 | + |
| 47 | +class MyTextField extends Text { |
| 48 | + static config = { |
| 49 | + className: 'Test.MyTextField' |
| 50 | + } |
| 51 | + |
| 52 | + afterSetValue(value, oldValue) { |
| 53 | + this.executionOrder.push('MyTextField.afterSetValue'); |
| 54 | + super.afterSetValue(value, oldValue); |
| 55 | + } |
| 56 | +} |
| 57 | +MyTextField = Neo.setupClass(MyTextField); |
| 58 | + |
| 59 | + |
| 60 | +test.describe('Neo.form.field.AfterSetValueSequence', () => { |
| 61 | + test('TextField afterSetValue sequence should fire change event after the full method chain', async () => { |
| 62 | + const executionOrder = []; |
| 63 | + |
| 64 | + const field = Neo.create(MyTextField, { |
| 65 | + // pass array to instance so it's available in afterSetValue |
| 66 | + executionOrder |
| 67 | + }); |
| 68 | + |
| 69 | + field.on('change', (changeEvent) => { |
| 70 | + executionOrder.push('change event'); |
| 71 | + expect(changeEvent.value).toBe('new value'); |
| 72 | + }); |
| 73 | + |
| 74 | + // Trigger the change |
| 75 | + field.value = 'new value'; |
| 76 | + |
| 77 | + // Assert the order |
| 78 | + expect(executionOrder).toEqual(['MyTextField.afterSetValue', 'MyTextField.afterSetValue', 'change event']); |
| 79 | + expect(field.value).toBe('new value'); |
| 80 | + |
| 81 | + field.destroy(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('ComboBox afterSetValue sequence should fire change event after the full method chain', async () => { |
| 85 | + const executionOrder = []; |
| 86 | + |
| 87 | + const field = Neo.create(MyComboBox, { |
| 88 | + // pass array to instance so it's available in afterSetValue |
| 89 | + executionOrder |
| 90 | + }); |
| 91 | + |
| 92 | + expect(Neo.isRecord(field.value)).toBe(true); |
| 93 | + expect(field.value.country).toBe('Angola'); |
| 94 | + |
| 95 | + field.on('change', (changeEvent) => { |
| 96 | + executionOrder.push('combo change event'); |
| 97 | + |
| 98 | + expect(Neo.isRecord(field.value)).toBe(true); |
| 99 | + expect(field.value.country).toBe('Algeria'); |
| 100 | + }); |
| 101 | + |
| 102 | + // Trigger the change |
| 103 | + field.value = 'Algeria'; |
| 104 | + |
| 105 | + expect(Neo.isRecord(field.value)).toBe(true); |
| 106 | + expect(field.value.country).toBe('Algeria'); |
| 107 | + |
| 108 | + // Required since change events are delayed in ComboBox |
| 109 | + await field.timeout(35); |
| 110 | + |
| 111 | + // Assert the order |
| 112 | + expect(executionOrder).toEqual(['MyComboBox.afterSetValue', 'MyComboBox.afterSetValue', 'combo change event']); |
| 113 | + |
| 114 | + expect(Neo.isRecord(field.value)).toBe(true); |
| 115 | + expect(field.value.country).toBe('Algeria'); |
| 116 | + |
| 117 | + field.destroy(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments