Skip to content

Commit 1020fdf

Browse files
committed
test(cell): add focused tests for shared state editing workflow
- Add TestWidgetWithSharedComponent mock implementing dashboardEditSharedState() - Test canEditSharedState() detection for widgets with/without method - Test onEditSharedState() properly invokes widget's shared state editor
1 parent b255c78 commit 1020fdf

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

projects/ngx-dashboard/src/lib/cell/__tests__/cell.component.spec.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ class TestWidgetComponent implements Widget {
3737
}
3838
}
3939

40+
// Mock test widget with shared state support
41+
@Component({
42+
selector: 'lib-test-widget-with-shared',
43+
template: '<div>Test Widget with Shared State</div>',
44+
standalone: true,
45+
})
46+
class TestWidgetWithSharedComponent implements Widget {
47+
private state = signal<unknown>({ value: 'test' });
48+
49+
dashboardGetState(): unknown {
50+
return this.state();
51+
}
52+
53+
dashboardSetState(state: unknown): void {
54+
this.state.set(state);
55+
}
56+
57+
dashboardEditState(): void {
58+
// Mock edit state method
59+
}
60+
61+
dashboardEditSharedState(): void {
62+
// Mock edit shared state method
63+
}
64+
}
65+
4066
describe('CellComponent - User Scenarios', () => {
4167
let component: CellComponent;
4268
let fixture: ComponentFixture<CellComponent>;
@@ -72,7 +98,7 @@ describe('CellComponent - User Scenarios', () => {
7298
mockRenderer = jasmine.createSpyObj('Renderer2', ['listen']);
7399

74100
await TestBed.configureTestingModule({
75-
imports: [CellComponent, TestWidgetComponent],
101+
imports: [CellComponent, TestWidgetComponent, TestWidgetWithSharedComponent],
76102
providers: [
77103
DashboardStore,
78104
{ provide: DashboardService, useValue: mockDashboardService },
@@ -210,6 +236,79 @@ describe('CellComponent - User Scenarios', () => {
210236
});
211237
});
212238

239+
describe('Shared State Editing Workflow', () => {
240+
beforeEach(() => {
241+
fixture.componentRef.setInput('widgetId', mockWidgetId);
242+
fixture.componentRef.setInput('cellId', mockCellId);
243+
fixture.componentRef.setInput('row', 1);
244+
fixture.componentRef.setInput('column', 1);
245+
fixture.componentRef.setInput('isEditMode', true);
246+
fixture.detectChanges();
247+
spyOn(component.edit, 'emit');
248+
});
249+
250+
it('should report if widget can edit shared state', async () => {
251+
// Create widget with shared state support
252+
const factoryWithShared = {
253+
...mockWidgetFactory,
254+
createInstance: jasmine
255+
.createSpy('createInstance')
256+
.and.callFake((container: ViewContainerRef, state?: unknown) => {
257+
const componentRef = container.createComponent(TestWidgetWithSharedComponent);
258+
if (state) {
259+
componentRef.instance.dashboardSetState(state);
260+
}
261+
return componentRef;
262+
}),
263+
};
264+
265+
fixture.componentRef.setInput('widgetFactory', factoryWithShared);
266+
fixture.detectChanges();
267+
await fixture.whenStable();
268+
269+
expect(component.canEditSharedState()).toBe(true);
270+
});
271+
272+
it('should report false for widgets without shared state editing capability', async () => {
273+
// Default TestWidgetComponent doesn't have dashboardEditSharedState
274+
await fixture.whenStable();
275+
expect(component.canEditSharedState()).toBe(false);
276+
});
277+
278+
it('should call widget shared state editor when method exists', async () => {
279+
// Create a mock component ref with spy
280+
const mockWidgetInstance = {
281+
dashboardGetState: jasmine.createSpy('dashboardGetState'),
282+
dashboardSetState: jasmine.createSpy('dashboardSetState'),
283+
dashboardEditState: jasmine.createSpy('dashboardEditState'),
284+
dashboardEditSharedState: jasmine.createSpy('dashboardEditSharedState'),
285+
};
286+
287+
const mockComponentRef = {
288+
instance: mockWidgetInstance,
289+
destroy: jasmine.createSpy('destroy'),
290+
};
291+
292+
// Create widget factory that returns our mock component ref
293+
const factoryWithShared = {
294+
...mockWidgetFactory,
295+
createInstance: jasmine
296+
.createSpy('createInstance')
297+
.and.returnValue(mockComponentRef as any),
298+
};
299+
300+
fixture.componentRef.setInput('widgetFactory', factoryWithShared);
301+
fixture.detectChanges();
302+
await fixture.whenStable();
303+
304+
// User invokes shared state editing
305+
component.onEditSharedState();
306+
307+
// Widget's shared state editor should be called
308+
expect(mockWidgetInstance.dashboardEditSharedState).toHaveBeenCalled();
309+
});
310+
});
311+
213312
describe('Settings Dialog Workflow', () => {
214313
beforeEach(() => {
215314
fixture.componentRef.setInput('widgetId', mockWidgetId);

0 commit comments

Comments
 (0)