Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions frameworks/angular-slickgrid/test/vitest-global-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,9 @@ window.HTMLElement.prototype.scrollIntoView = () => {};

Object.defineProperty(window, 'localStorage', { value: mock() });
Object.defineProperty(window, 'sessionStorage', { value: mock() });
Object.defineProperty(window, 'getComputedStyle', {
value: () => ['-webkit-appearance'],
});

Object.defineProperty(window, '__env', { value: { env: { backendUrl: 'mocked URL' } } });

Object.defineProperty(window, 'getComputedStyle', {
value: () => ({
getPropertyValue: () => {
return '';
},
}),
});

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(function (query) {
Expand Down
9 changes: 6 additions & 3 deletions frameworks/angular-slickgrid/test/vitest-pretest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'jsdom-global/register';

// (global as any).Storage = window.localStorage;
(global as any).navigator = { userAgent: 'node.js' };
if (!globalThis.navigator) {
Object.defineProperty(globalThis, 'navigator', {
value: { userAgent: 'node.js' },
configurable: true,
});
}
14 changes: 5 additions & 9 deletions packages/common/src/core/__tests__/slickCore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,17 @@ describe('SlickCore file', () => {

it('should be able to add a PubSub instance to the SlickEvent call notify() and expect PubSub .publish() to be called and the externalize event callback be called also', () => {
const ed = new SlickEventData();
const pubSubCopy = { ...pubSubServiceStub };
const publishSpy = vi.fn();
const pubSubCopy = { ...pubSubServiceStub, publish: publishSpy };
const onClick = new SlickEvent('onClick', pubSubCopy);
pubSubCopy.publish = (_evtName, _data, _delay, evtCallback) => {
publishSpy.mockImplementation((_evtName, _data, _delay, evtCallback) => {
evtCallback!(new CustomEvent('click'));
};
});

onClick.notify({ hello: 'world' }, ed);

expect(ed.nativeEvent).toBeDefined();
expect(pubSubServiceStub.publish).toHaveBeenCalledWith(
'onClick',
{ eventData: expect.any(Object), args: { hello: 'world' } },
undefined,
expect.any(Function)
);
expect(publishSpy).toHaveBeenCalledWith('onClick', { eventData: expect.any(Object), args: { hello: 'world' } }, undefined, expect.any(Function));
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('Draggable class', () => {
target: document.body,
});
expect(dragPos).toEqual(dragStartPos);
expect(dragEndPos).toEqual({ ...dragStartPos, target: window });
expect(dragEndPos).toEqual({ ...dragStartPos, target: expect.any(Window) });
expect(removeBodyListenerSpy).toHaveBeenCalledTimes(2 * 2); // 2x events
expect(removeWindowListenerSpy).toHaveBeenCalledTimes(3 * 2); // 3x events
});
Expand Down
23 changes: 13 additions & 10 deletions packages/common/src/extensions/__tests__/rowMoveUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { SlickEvent, type SlickGrid } from '../../core/index.js';
import { SlickEvent, type SlickDataView, type SlickGrid } from '../../core/index.js';
import { defaultOnBeforeMoveRows, defaultOnMoveRows } from '../rowMoveUtils.js';

type DataViewStub = Omit<SlickDataView, 'getItemCount'> & {
getItemCount?: SlickDataView['getItemCount'];
};

const dataViewStub = {
destroy: vi.fn(),
addItem: vi.fn(),
Expand All @@ -12,7 +16,7 @@ const dataViewStub = {
getItemCount: vi.fn(),
setItems: vi.fn(),
sort: vi.fn(),
} as unknown as SlickDataView;
} as unknown as DataViewStub;

const gridStub = {
getCellNode: vi.fn(),
Expand All @@ -35,19 +39,19 @@ describe('rowMoveUtils', () => {

it('should return false when trying to move at same position', () => {
vi.spyOn(dataViewStub, 'getItemCount').mockReturnValue(4);
const output = defaultOnBeforeMoveRows(new CustomEvent('change'), { rows: [0, 1, 2, 3], insertBefore: 1, grid: gridStub });
const output = defaultOnBeforeMoveRows(new MouseEvent('change'), { rows: [0, 1, 2, 3], insertBefore: 1, grid: gridStub });
expect(output).toEqual(false);
});

it('should return true when trying to move at available spot', () => {
vi.spyOn(dataViewStub, 'getItemCount').mockReturnValue(500);
const output = defaultOnBeforeMoveRows(new CustomEvent('change'), { rows: [1], insertBefore: 3, grid: gridStub });
const output = defaultOnBeforeMoveRows(new MouseEvent('change'), { rows: [1], insertBefore: 3, grid: gridStub });
expect(output).toEqual(true);
});

it('should return false when dataView.getItemCount() is undefined', () => {
dataViewStub.getItemCount = undefined;
const output = defaultOnBeforeMoveRows(new CustomEvent('change'), { rows: [1], insertBefore: 3, grid: gridStub });
const output = defaultOnBeforeMoveRows(new MouseEvent('change'), { rows: [1], insertBefore: 3, grid: gridStub });
expect(output).toEqual(false);
});
});
Expand All @@ -70,7 +74,7 @@ describe('rowMoveUtils', () => {
vi.spyOn(dataViewStub, 'getItem').mockReturnValue(items[1]);
vi.spyOn(dataViewStub, 'getIdxById').mockReturnValue(0).mockReturnValueOnce(0).mockReturnValueOnce(1).mockReturnValueOnce(2).mockReturnValueOnce(3);
vi.spyOn(dataViewStub, 'getItemCount').mockReturnValue(items.length);
defaultOnMoveRows(new CustomEvent('change'), { insertBefore: 2, rows: [0, 1, 2, 3], grid: gridStub });
defaultOnMoveRows(new MouseEvent('change'), { insertBefore: 2, rows: [0, 1, 2, 3], grid: gridStub });

expect(setItemSpy).toHaveBeenCalledWith([
{ id: 1, firstName: 'Jane' },
Expand All @@ -94,21 +98,20 @@ describe('rowMoveUtils', () => {
vi.spyOn(dataViewStub, 'getItem').mockReturnValue(null);
vi.spyOn(dataViewStub, 'getIdxById').mockReturnValue(0).mockReturnValueOnce(0).mockReturnValueOnce(1).mockReturnValueOnce(2).mockReturnValueOnce(3);
vi.spyOn(dataViewStub, 'getItemCount').mockReturnValue(items.length);
defaultOnMoveRows(new CustomEvent('change'), { insertBefore: 2, rows: [0, 1, 2, 3], grid: gridStub });
defaultOnMoveRows(new MouseEvent('change'), { insertBefore: 2, rows: [0, 1, 2, 3], grid: gridStub });

expect(setItemSpy).toHaveBeenCalledWith([
{ id: 0, firstName: 'John' },
{ id: 1, firstName: 'Jane' },
{ id: 2, firstName: 'Smith' },
{ id: 3, firstName: 'Bob' },
{ id: 0, firstName: 'John' },
{ id: 1, firstName: 'Jane' },
]);
});

it('should return false when dataView.getItemCount() is undefined', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockReturnValue();
dataViewStub.getItemCount = undefined;
defaultOnMoveRows(new CustomEvent('change'), { rows: [1], insertBefore: 3, grid: gridStub });
defaultOnMoveRows(new MouseEvent('change'), { rows: [1], insertBefore: 3, grid: gridStub });
expect(consoleErrorSpy).toHaveBeenCalledWith('Sorry `defaultOnMoveRows()` only works with SlickDataView');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ describe('GridMenuControl', () => {
// Verify slotRenderer was called with the click event as the third argument
// GridMenu calls slotRenderer 3 times: init, openMenu, and click
expect(mockSlotRenderer).toHaveBeenCalledTimes(3);
const clickCallArgs = mockSlotRenderer.mock.calls[2]; // third call is from click
const clickCallArgs: any[] = mockSlotRenderer.mock.calls[2]; // third call is from click
expect(clickCallArgs[2]).toBeDefined();
expect(clickCallArgs[2]!.type).toBe('click');
});
Expand Down Expand Up @@ -2774,7 +2774,7 @@ describe('GridMenuControl', () => {
const labelForcefitElm = control.menuElement!.querySelector('label[for=slickgrid_124343-gridmenu-colpicker-forcefit]') as HTMLLabelElement;
const labelSyncElm = control.menuElement!.querySelector('label[for=slickgrid_124343-gridmenu-colpicker-syncresize]') as HTMLLabelElement;

expect(handlerSpy).toHaveBeenCalledTimes(4 * 2);
expect(handlerSpy).toHaveBeenCalledTimes(4);
// expect(commandTitleElm.textContent).toBe('Custom Command Title');
expect(columnTitleElm.textContent).toBe('Custom Column Title');
expect(labelForcefitElm.textContent).toBe('Custom Force Fit Title');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,12 @@ describe('formatterUtilities', () => {
};

// Mock clipboard API
global.navigator = {
clipboard: {
Object.defineProperty(globalThis.navigator, 'clipboard', {
value: {
writeText: clipboardWriteMock,
} as any,
} as any;
configurable: true,
});

// Clear all mocks before each test
vi.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ describe('GridStateService', () => {

it('should have called the "subscribeToAllGridChanges" method while initializing with Hybrid Selection is enabled', () => {
vi.spyOn(gridStub, 'getSelectionModel').mockReturnValueOnce(hybridSelectionModelStub);
vi.clearAllMocks();
const gridStateSpy = vi.spyOn(service, 'subscribeToAllGridChanges');
const pubSubSpy = vi.spyOn(mockPubSub, 'subscribe');

Expand All @@ -174,7 +175,7 @@ describe('GridStateService', () => {
vi.spyOn(gridStub, 'getSelectionModel').mockReturnValueOnce(hybridSelectionModelStub);

expect(gridStateSpy).toHaveBeenCalled();
expect(pubSubSpy).toHaveBeenCalledTimes(21); // not 7 but 21 with hybrid selection, not sure why though
expect(pubSubSpy).toHaveBeenCalledTimes(7);
// expect(pubSubSpy).toHaveBeenNthCalledWith(1, `onFilterChanged`, () => { });
});

Expand Down
Loading
Loading