|
| 1 | +import '../setup.mjs'; // Sets up the globalThis.Neo object |
| 2 | + |
| 3 | +import { test, expect } from '@playwright/test'; |
| 4 | + |
| 5 | +// Correct import order is critical for Neo.mjs initialization |
| 6 | +import Neo from '../../../src/Neo.mjs'; |
| 7 | +import * as core from '../../../src/core/_export.mjs'; |
| 8 | +import Button from '../../../src/button/Base.mjs'; |
| 9 | +import DomApiVnodeCreator from '../../../src/vdom/util/DomApiVnodeCreator.mjs'; |
| 10 | +import VdomHelper from '../../../src/vdom/Helper.mjs'; |
| 11 | + |
| 12 | + |
| 13 | +const appName = 'ClassicButtonTest'; |
| 14 | + |
| 15 | +test.describe('Neo.button.Base VDOM (Node.js)', () => { |
| 16 | + |
| 17 | + test('should create initial vnode correctly', async () => { |
| 18 | + const button = Neo.create(Button, { |
| 19 | + appName, |
| 20 | + iconCls: 'fa fa-home', |
| 21 | + text : 'Click me' |
| 22 | + }); |
| 23 | + const { vnode } = await button.initVnode(); |
| 24 | + button.destroy(); |
| 25 | + |
| 26 | + expect(vnode.nodeName).toBe('button'); |
| 27 | + expect(vnode.className).toEqual(['neo-button', 'icon-left']); |
| 28 | + expect(vnode.childNodes.length).toBe(2); |
| 29 | + |
| 30 | + const iconNode = vnode.childNodes[0]; |
| 31 | + expect(iconNode.className).toEqual(['neo-button-glyph', 'fa', 'fa-home']); |
| 32 | + |
| 33 | + const textNode = vnode.childNodes[1]; |
| 34 | + expect(textNode.className).toEqual(['neo-button-text']); |
| 35 | + expect(textNode.textContent).toBe('Click me'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('should update vnode and create delta for a single config change', async () => { |
| 39 | + const button = Neo.create(Button, { |
| 40 | + appName, |
| 41 | + text: 'Click me' |
| 42 | + }); |
| 43 | + await button.initVnode(); |
| 44 | + button.mounted = true; |
| 45 | + |
| 46 | + const textNodeId = button.vnode.childNodes[0].id; |
| 47 | + const { deltas } = await button.set({text: 'New Text'}); |
| 48 | + button.destroy(); |
| 49 | + |
| 50 | + expect(deltas.length).toBe(1); |
| 51 | + const delta = deltas[0]; |
| 52 | + expect(delta.id).toBe(textNodeId); |
| 53 | + expect(delta.textContent).toBe('New Text'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('should update vnode and create delta for multiple config changes', async () => { |
| 57 | + const button = Neo.create(Button, { |
| 58 | + appName, |
| 59 | + iconCls: 'fa fa-home', |
| 60 | + text : 'Click me' |
| 61 | + }); |
| 62 | + await button.initVnode(); |
| 63 | + button.mounted = true; |
| 64 | + |
| 65 | + const iconNodeId = button.vnode.childNodes[0].id; |
| 66 | + const textNodeId = button.vnode.childNodes[1].id; |
| 67 | + |
| 68 | + const { deltas } = await button.set({ |
| 69 | + iconCls: 'fa fa-user', |
| 70 | + text : 'Submit' |
| 71 | + }); |
| 72 | + button.destroy(); |
| 73 | + |
| 74 | + expect(deltas.length).toBe(2); |
| 75 | + |
| 76 | + const iconDelta = deltas.find(d => d.id === iconNodeId); |
| 77 | + const textDelta = deltas.find(d => d.id === textNodeId); |
| 78 | + |
| 79 | + expect(iconDelta).toBeDefined(); |
| 80 | + expect(iconDelta.cls.remove).toEqual(['fa-home']); |
| 81 | + expect(iconDelta.cls.add).toEqual(['fa-user']); |
| 82 | + |
| 83 | + expect(textDelta).toBeDefined(); |
| 84 | + expect(textDelta.textContent).toBe('Submit'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should handle pressed state change', async () => { |
| 88 | + const button = Neo.create(Button, { |
| 89 | + appName |
| 90 | + }); |
| 91 | + const {vnode} = await button.initVnode(); |
| 92 | + button.mounted = true; |
| 93 | + |
| 94 | + expect(vnode.className.includes('pressed')).toBe(false); |
| 95 | + |
| 96 | + let updateData = await button.set({pressed: true}); |
| 97 | + expect(updateData.deltas.length).toBe(1); |
| 98 | + let delta = updateData.deltas[0]; |
| 99 | + expect(delta.id).toBe(button.id); |
| 100 | + expect(delta.cls.add).toEqual(['pressed']); |
| 101 | + expect(updateData.vnode.className.includes('pressed')).toBe(true); |
| 102 | + |
| 103 | + updateData = await button.set({pressed: false}); |
| 104 | + expect(updateData.deltas.length).toBe(1); |
| 105 | + delta = updateData.deltas[0]; |
| 106 | + expect(delta.id).toBe(button.id); |
| 107 | + expect(delta.cls.remove).toEqual(['pressed']); |
| 108 | + expect(updateData.vnode.className.includes('pressed')).toBe(false); |
| 109 | + |
| 110 | + button.destroy(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments