From 54e4c84d8a733752c6440e84c0b7ca94dfb46129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 9 May 2024 14:23:36 +0200 Subject: [PATCH 1/2] Typescript: Enable strict mode --- .../src/querying/SceneDataTransformer.test.ts | 4 +- .../src/querying/SceneQueryRunner.test.ts | 4 +- .../adhoc/AdHocFiltersVariable.test.tsx | 38 +++++++++---------- tsconfig.json | 17 +++------ 4 files changed, 26 insertions(+), 37 deletions(-) diff --git a/packages/scenes/src/querying/SceneDataTransformer.test.ts b/packages/scenes/src/querying/SceneDataTransformer.test.ts index 834444715..f0bbd322a 100644 --- a/packages/scenes/src/querying/SceneDataTransformer.test.ts +++ b/packages/scenes/src/querying/SceneDataTransformer.test.ts @@ -543,9 +543,9 @@ describe('SceneDataTransformer', () => { const data = stateUpdates[0].data; // Verify series are transformed - expect(data.series[0].fields[0].values[0]).toBe(10 * 2); + expect(data?.series[0].fields[0].values[0]).toBe(10 * 2); // Verify annotations are transformed - expect(data.annotations[0].fields[0].values[0]).toBe(100 + 4); + expect(data?.annotations?.[0].fields[0].values[0]).toBe(100 + 4); }); describe('With inner query runner', () => { diff --git a/packages/scenes/src/querying/SceneQueryRunner.test.ts b/packages/scenes/src/querying/SceneQueryRunner.test.ts index 221ab42c9..a2956c858 100644 --- a/packages/scenes/src/querying/SceneQueryRunner.test.ts +++ b/packages/scenes/src/querying/SceneQueryRunner.test.ts @@ -2062,10 +2062,10 @@ describe('SceneQueryRunner', () => { const clone = queryRunner.clone(); expect(clone['_resultAnnotations']).not.toBeUndefined(); - expect(clone['_resultAnnotations'].length).toBe(1); + expect(clone['_resultAnnotations']?.length).toBe(1); expect(clone['_resultAnnotations']).toStrictEqual(queryRunner['_resultAnnotations']); expect(clone['_layerAnnotations']).not.toBeUndefined(); - expect(clone['_layerAnnotations'].length).toBe(1); + expect(clone['_layerAnnotations']?.length).toBe(1); expect(clone['_layerAnnotations']).toStrictEqual(queryRunner['_layerAnnotations']); expect(clone['_results']['_buffer']).not.toEqual([]); }); diff --git a/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx b/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx index 062adafc1..1f7cee92d 100644 --- a/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx +++ b/packages/scenes/src/variables/adhoc/AdHocFiltersVariable.test.tsx @@ -103,17 +103,17 @@ describe('AdHocFiltersVariable', () => { }); it('Can set a custom value before the list of values returns', async () => { - let resolveCallback; - const delayingPromise = new Promise((resolve) => resolveCallback = resolve); + let resolveCallback = (v: string) => {}; + const delayingPromise = new Promise((resolve) => (resolveCallback = resolve)); const { filtersVar, runRequest } = setup({ getTagValuesProvider: async () => { await delayingPromise; return { replace: true, - values: [{ text: 'Value 3', value: 'value3' }] - } - } + values: [{ text: 'Value 3', value: 'value3' }], + }; + }, }); await new Promise((r) => setTimeout(r, 1)); @@ -126,11 +126,11 @@ describe('AdHocFiltersVariable', () => { await userEvent.type(selects[2], 'myVeryCustomValue'); // resolve the delaying promise - await act(resolveCallback); + act(() => resolveCallback('')); await userEvent.type(selects[2], '{enter}'); - // check the value has been set + // check the value has been set expect(runRequest.mock.calls.length).toBe(2); expect(filtersVar.state.filters[0].value).toBe('myVeryCustomValue'); @@ -138,11 +138,10 @@ describe('AdHocFiltersVariable', () => { expect(screen.queryByText('Value 3')).not.toBeInTheDocument(); }); - describe('By default, Without altering `useQueriesAsFilterForOptions`', ()=>{ - + describe('By default, Without altering `useQueriesAsFilterForOptions`', () => { it('Should not collect and pass respective data source queries to getTagKeys call', async () => { const { getTagKeysSpy, timeRange } = setup({ filters: [] }); - + // Select key await userEvent.click(screen.getByTestId('AdHocFilter-add')); expect(getTagKeysSpy).toBeCalledTimes(1); @@ -152,17 +151,17 @@ describe('AdHocFiltersVariable', () => { timeRange: timeRange.state.value, }); }); - + it('Should not collect and pass respective data source queries to getTagValues call', async () => { const { getTagValuesSpy, timeRange } = setup({ filters: [] }); - + // Select key const key = 'Key 3'; await userEvent.click(screen.getByTestId('AdHocFilter-add')); const selects = getAllByRole(screen.getByTestId('AdHocFilter-'), 'combobox'); await waitFor(() => select(selects[0], key, { container: document.body })); await userEvent.click(selects[2]); - + expect(getTagValuesSpy).toBeCalledTimes(1); expect(getTagValuesSpy).toBeCalledWith({ filters: [], @@ -173,11 +172,10 @@ describe('AdHocFiltersVariable', () => { }); }); - describe('When `useQueriesAsFilterForOptions` is set to `true`', ()=>{ - + describe('When `useQueriesAsFilterForOptions` is set to `true`', () => { it('Should collect and pass respective data source queries to getTagKeys call', async () => { const { getTagKeysSpy, timeRange } = setup({ filters: [], useQueriesAsFilterForOptions: true }); - + // Select key await userEvent.click(screen.getByTestId('AdHocFilter-add')); expect(getTagKeysSpy).toBeCalledTimes(1); @@ -192,17 +190,17 @@ describe('AdHocFiltersVariable', () => { timeRange: timeRange.state.value, }); }); - + it('Should collect and pass respective data source queries to getTagValues call', async () => { const { getTagValuesSpy, timeRange } = setup({ filters: [], useQueriesAsFilterForOptions: true }); - + // Select key const key = 'Key 3'; await userEvent.click(screen.getByTestId('AdHocFilter-add')); const selects = getAllByRole(screen.getByTestId('AdHocFilter-'), 'combobox'); await waitFor(() => select(selects[0], key, { container: document.body })); await userEvent.click(selects[2]); - + expect(getTagValuesSpy).toBeCalledTimes(1); expect(getTagValuesSpy).toBeCalledWith({ filters: [], @@ -216,10 +214,8 @@ describe('AdHocFiltersVariable', () => { timeRange: timeRange.state.value, }); }); - }); - it('url sync works', async () => { const { filtersVar } = setup(); diff --git a/tsconfig.json b/tsconfig.json index 6df2199bc..5d0e70111 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,25 +1,18 @@ { - "extends": "@grafana/tsconfig/base.json", + "extends": "@grafana/tsconfig/tsconfig.json", "compilerOptions": { "declaration": true, "declarationDir": "./compiled", "emitDeclarationOnly": true, "isolatedModules": true, - "rootDirs": [ - "." - ] + "rootDirs": ["."] }, - "exclude": [ - "dist/**/*" - ], - "include": [ - "utils/**/*.ts*", - "src/**/*.ts*" - ], + "exclude": ["dist/**/*"], + "include": ["utils/**/*.ts*", "src/**/*.ts*"], "ts-node": { "compilerOptions": { "isolatedModules": false, "module": "commonjs" } } -} \ No newline at end of file +} From e8873cc08ee132da6cd8973ded97ec7450850722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sun, 12 May 2024 17:03:25 +0200 Subject: [PATCH 2/2] Update --- packages/scenes/src/querying/SceneDataTransformer.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/scenes/src/querying/SceneDataTransformer.test.ts b/packages/scenes/src/querying/SceneDataTransformer.test.ts index 80cd5d7f9..8d10b4df4 100644 --- a/packages/scenes/src/querying/SceneDataTransformer.test.ts +++ b/packages/scenes/src/querying/SceneDataTransformer.test.ts @@ -700,7 +700,7 @@ describe('SceneDataTransformer', () => { dataNode.setState({ data: { ...dataNode.state.data, state: LoadingState.Loading } }); expect(customTransformerSpy).toHaveBeenCalledTimes(1); - expect(transformer.state.data.state).toBe(LoadingState.Loading); + expect(transformer.state.data?.state).toBe(LoadingState.Loading); }); }); });