|
| 1 | +import {LitElement} from 'lit'; |
| 2 | +import {customElement, state} from 'lit/decorators.js'; |
| 3 | +import {beforeEach, describe, expect, it, vi} from 'vitest'; |
| 4 | +import { |
| 5 | + FoldedItemListContextController, |
| 6 | + MissingParentError, |
| 7 | +} from './folded-item-list-context-controller'; |
| 8 | + |
| 9 | +@customElement('test-element') |
| 10 | +class TestElement extends LitElement { |
| 11 | + @state() error!: Error; |
| 12 | + |
| 13 | + requestUpdate = vi.fn(); |
| 14 | +} |
| 15 | + |
| 16 | +describe('folded-item-list-context', () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('#MissingParentError', () => { |
| 22 | + it('should create error with correct message when provided element name', () => { |
| 23 | + const error = new MissingParentError('child-element'); |
| 24 | + |
| 25 | + expect(error.message).toBe( |
| 26 | + 'The "child-element" element must be the child of an "atomic-folded-result-list" or "atomic-insight-folded-result-list" element.' |
| 27 | + ); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('#FoldedItemListContextController', () => { |
| 32 | + let mockElement: TestElement; |
| 33 | + let controller: FoldedItemListContextController; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + mockElement = new TestElement(); |
| 37 | + vi.spyOn(mockElement, 'addController'); |
| 38 | + vi.spyOn(mockElement, 'dispatchEvent'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should register itself as a controller with the host', () => { |
| 42 | + controller = new FoldedItemListContextController(mockElement); |
| 43 | + |
| 44 | + expect(mockElement.addController).toHaveBeenCalledWith(controller); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('when controller is connected', () => { |
| 48 | + beforeEach(() => { |
| 49 | + controller = new FoldedItemListContextController(mockElement); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should dispatch the resolveFoldedResultList event in #hostConnected', () => { |
| 53 | + vi.spyOn(mockElement, 'dispatchEvent').mockReturnValue(false); |
| 54 | + |
| 55 | + controller.hostConnected(); |
| 56 | + |
| 57 | + expect(mockElement.dispatchEvent).toHaveBeenCalledWith( |
| 58 | + expect.objectContaining({ |
| 59 | + type: 'atomic/resolveFoldedResultList', |
| 60 | + }) |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('when event is not canceled', () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.spyOn(mockElement, 'dispatchEvent').mockImplementation((event) => { |
| 67 | + const customEvent = event as CustomEvent; |
| 68 | + const handler = customEvent.detail; |
| 69 | + if (typeof handler === 'function') { |
| 70 | + handler({ |
| 71 | + logShowMoreFoldedResults: vi.fn(), |
| 72 | + logShowLessFoldedResults: vi.fn(), |
| 73 | + }); |
| 74 | + } |
| 75 | + return false; |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should set folded item list data', () => { |
| 80 | + controller.hostConnected(); |
| 81 | + |
| 82 | + expect(controller.foldedItemList).toEqual({ |
| 83 | + logShowMoreFoldedResults: expect.any(Function), |
| 84 | + logShowLessFoldedResults: expect.any(Function), |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should clear error', () => { |
| 89 | + controller.hostConnected(); |
| 90 | + |
| 91 | + expect(controller.error).toBeNull(); |
| 92 | + expect(controller.hasError).toBe(false); |
| 93 | + expect(mockElement.error).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should request update', () => { |
| 97 | + controller.hostConnected(); |
| 98 | + |
| 99 | + expect(mockElement.requestUpdate).toHaveBeenCalledTimes(1); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('when event is canceled', () => { |
| 104 | + beforeEach(() => { |
| 105 | + vi.spyOn(mockElement, 'dispatchEvent').mockReturnValue(true); |
| 106 | + Object.defineProperty(mockElement, 'nodeName', { |
| 107 | + value: 'TEST-ELEMENT', |
| 108 | + configurable: true, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should set error', () => { |
| 113 | + controller.hostConnected(); |
| 114 | + |
| 115 | + expect(controller.error).toBeInstanceOf(MissingParentError); |
| 116 | + expect(controller.hasError).toBe(true); |
| 117 | + expect(controller.error?.message).toBe( |
| 118 | + 'The "test-element" element must be the child of an "atomic-folded-result-list" or "atomic-insight-folded-result-list" element.' |
| 119 | + ); |
| 120 | + expect(mockElement.error).toBe(controller.error); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should clear folded item list data', () => { |
| 124 | + controller.hostConnected(); |
| 125 | + |
| 126 | + expect(controller.foldedItemList).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should request update', () => { |
| 130 | + controller.hostConnected(); |
| 131 | + |
| 132 | + expect(mockElement.requestUpdate).toHaveBeenCalledTimes(2); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('#foldedItemList getter', () => { |
| 137 | + beforeEach(() => { |
| 138 | + controller = new FoldedItemListContextController(mockElement); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should return null when there is an error', () => { |
| 142 | + vi.spyOn(mockElement, 'dispatchEvent').mockReturnValue(true); |
| 143 | + Object.defineProperty(mockElement, 'nodeName', { |
| 144 | + value: 'TEST-ELEMENT', |
| 145 | + configurable: true, |
| 146 | + }); |
| 147 | + |
| 148 | + controller.hostConnected(); |
| 149 | + |
| 150 | + expect(controller.foldedItemList).toBeNull(); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should return folded item list data when there is no error', () => { |
| 154 | + const mockFoldedItemList = { |
| 155 | + logShowMoreFoldedResults: vi.fn(), |
| 156 | + logShowLessFoldedResults: vi.fn(), |
| 157 | + }; |
| 158 | + |
| 159 | + vi.spyOn(mockElement, 'dispatchEvent').mockImplementation((event) => { |
| 160 | + const customEvent = event as CustomEvent; |
| 161 | + const handler = customEvent.detail; |
| 162 | + if (typeof handler === 'function') { |
| 163 | + handler(mockFoldedItemList); |
| 164 | + } |
| 165 | + return false; |
| 166 | + }); |
| 167 | + |
| 168 | + controller.hostConnected(); |
| 169 | + |
| 170 | + expect(controller.foldedItemList).toBe(mockFoldedItemList); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments