|
| 1 | +/* eslint-disable no-restricted-syntax */ |
| 2 | +import { domBackend } from '../base/env' |
| 3 | +import * as glassEasel from '../../src' |
| 4 | + |
| 5 | +const componentSpace = new glassEasel.ComponentSpace() |
| 6 | +componentSpace.updateComponentOptions({ |
| 7 | + writeFieldsToNode: true, |
| 8 | +}) |
| 9 | +componentSpace.defineComponent({ |
| 10 | + is: '', |
| 11 | +}) |
| 12 | + |
| 13 | +describe('Relation', () => { |
| 14 | + test('should link when another component is lazily loaded', () => { |
| 15 | + const childLinkedList: ['linked' | 'unlinked', glassEasel.GeneralComponent][] = [] |
| 16 | + const childDef = componentSpace.defineComponent({ |
| 17 | + is: 'child', |
| 18 | + relations: { |
| 19 | + parent: { |
| 20 | + type: 'ancestor', |
| 21 | + linked(target) { |
| 22 | + childLinkedList.push(['linked', target]) |
| 23 | + }, |
| 24 | + unlinked(target) { |
| 25 | + childLinkedList.push(['unlinked', target]) |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }) |
| 30 | + const child = glassEasel.Component.createWithContext('child', childDef, domBackend) |
| 31 | + |
| 32 | + expect(childLinkedList).toEqual([]) |
| 33 | + |
| 34 | + const parentLinkedList: ['linked' | 'unlinked', glassEasel.GeneralComponent][] = [] |
| 35 | + const parentDef = componentSpace.defineComponent({ |
| 36 | + is: 'parent', |
| 37 | + relations: { |
| 38 | + child: { |
| 39 | + type: 'descendant', |
| 40 | + linked(target) { |
| 41 | + parentLinkedList.push(['linked', target]) |
| 42 | + }, |
| 43 | + unlinked(target) { |
| 44 | + parentLinkedList.push(['unlinked', target]) |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | + const parent = glassEasel.Component.createWithContext('parent', parentDef, domBackend) |
| 50 | + glassEasel.Element.pretendAttached(parent) |
| 51 | + |
| 52 | + expect(childLinkedList).toStrictEqual([]) |
| 53 | + expect(parentLinkedList).toStrictEqual([]) |
| 54 | + |
| 55 | + parent.appendChild(child) |
| 56 | + |
| 57 | + expect(childLinkedList).toStrictEqual([['linked', parent]]) |
| 58 | + expect(parentLinkedList).toStrictEqual([['linked', child]]) |
| 59 | + |
| 60 | + parent.removeChild(child) |
| 61 | + |
| 62 | + expect(childLinkedList).toStrictEqual([ |
| 63 | + ['linked', parent], |
| 64 | + ['unlinked', parent], |
| 65 | + ]) |
| 66 | + expect(parentLinkedList).toStrictEqual([ |
| 67 | + ['linked', child], |
| 68 | + ['unlinked', child], |
| 69 | + ]) |
| 70 | + }) |
| 71 | +}) |
0 commit comments