Skip to content

Commit

Permalink
Clean up based on review
Browse files Browse the repository at this point in the history
- Create utility functions to make tests more readable
- Clean up some comments
  • Loading branch information
mofojed committed May 24, 2024
1 parent 29a7339 commit 0708745
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
26 changes: 8 additions & 18 deletions plugins/ui/src/js/src/widget/WidgetHandler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import WidgetHandler, { WidgetHandlerProps } from './WidgetHandler';
import { DocumentHandlerProps } from './DocumentHandler';
import {
makeDocumentUpdatedJsonRpcString,
makeSetStateResponse,
makeJsonRpcResponseString,
makeWidget,
makeWidgetDescriptor,
makeWidgetEventDocumentUpdated,
makeWidgetEventJsonRpcResponse,
} from './WidgetTestUtils';

const mockApi = { Widget: { EVENT_MESSAGE: 'message' } };
Expand Down Expand Up @@ -97,7 +99,7 @@ it('updates the document when event is received', async () => {
// Respond to the setState call first
listener({
detail: {
getDataAsString: jest.fn(() => makeSetStateResponse(1, {})),
getDataAsString: jest.fn(() => makeJsonRpcResponseString(1, {})),
exportedObjects: [],
},
});
Expand Down Expand Up @@ -203,22 +205,10 @@ it('updates the initial data only when fetch has changed', async () => {
// Send the initial document
await act(async () => {
// Respond to the setState call first
listener({
detail: {
getDataAsString: jest.fn(() => makeSetStateResponse(1, {})),
exportedObjects: [],
},
});
listener(makeWidgetEventJsonRpcResponse(1));

// Then send the initial document update
listener({
detail: {
getDataAsString: jest.fn(() =>
makeDocumentUpdatedJsonRpcString(document1)
),
exportedObjects: [],
},
});
listener(makeWidgetEventDocumentUpdated(document1));
});

expect(mockDocumentHandler).toHaveBeenCalledWith(
Expand Down Expand Up @@ -261,7 +251,7 @@ it('updates the initial data only when fetch has changed', async () => {
expect(fetch1).not.toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();

// Re-render with the fetch changed, it should set the state with the updated data
// Re-render with the widget descriptor changed, it should set the state with the updated data
rerender(
makeWidgetHandler({
widget: widget2,
Expand Down Expand Up @@ -300,7 +290,7 @@ it('updates the initial data only when fetch has changed', async () => {
// Respond to the setState call first
listener({
detail: {
getDataAsString: jest.fn(() => makeSetStateResponse(1, {})),
getDataAsString: jest.fn(() => makeJsonRpcResponseString(1, {})),
exportedObjects: [],
},
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/ui/src/js/src/widget/WidgetHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function WidgetHandler({
const [widget, setWidget] = useState<dh.Widget>();
const [document, setDocument] = useState<ReactNode>();
const [error, setError] = useState<WidgetError>();
// const [initialData] = useState(initialDataProp);

// We want to update the initial data if the widget changes, as we'll need to re-fetch the widget and want to start with a fresh state.
// eslint-disable-next-line react-hooks/exhaustive-deps
const initialData = useMemo(() => initialDataProp, [widget]);
Expand Down
31 changes: 26 additions & 5 deletions plugins/ui/src/js/src/widget/WidgetTestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WidgetDescriptor } from '@deephaven/dashboard';
import { TestUtils } from '@deephaven/utils';
import type { dh } from '@deephaven/jsapi-types';
import { WidgetMessageEvent } from './WidgetTypes';

export function makeDocumentUpdatedJsonRpc(
document: Record<string, unknown> = {}
Expand All @@ -12,14 +13,11 @@ export function makeDocumentUpdatedJsonRpc(
};
}

export function makeSetStateResponse(
id: number,
state: Record<string, unknown>
): string {
export function makeJsonRpcResponseString(id: number, result = ''): string {
return JSON.stringify({
jsonrpc: '2.0',
id,
result: '',
result,
});
}

Expand All @@ -29,6 +27,29 @@ export function makeDocumentUpdatedJsonRpcString(
return JSON.stringify(makeDocumentUpdatedJsonRpc(document));
}

export function makeWidgetEvent(data = ''): WidgetMessageEvent {
return new CustomEvent('message', {
detail: {
getDataAsBase64: () => '',
getDataAsString: () => data,
exportedObjects: [],
},
});
}

export function makeWidgetEventJsonRpcResponse(
id: number,
response = ''
): WidgetMessageEvent {
return makeWidgetEvent(makeJsonRpcResponseString(id, response));
}

export function makeWidgetEventDocumentUpdated(
document: Record<string, unknown> = {}
): WidgetMessageEvent {
return makeWidgetEvent(makeDocumentUpdatedJsonRpcString(document));
}

export function makeWidgetDescriptor({
id = 'widget-id',
type = 'widget-type',
Expand Down

0 comments on commit 0708745

Please sign in to comment.