Skip to content

Commit

Permalink
fix: remove circular dependencies from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leventebalogh committed Dec 17, 2024
1 parent c46506d commit 8ee5f00
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 43 deletions.
12 changes: 8 additions & 4 deletions packages/scenes/src/components/SceneApp/SceneApp.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NavModelItem } from '@grafana/data';
import { locationService, PluginPage } from '@grafana/runtime';
import { locationService, PluginPage, PluginPageProps } from '@grafana/runtime';
import { screen, render } from '@testing-library/react';
import React from 'react';
import { renderAppInsideRouterWithStartingUrl } from '../../../utils/test/utils';
import { renderAppInsideRouterWithStartingUrl } from '../../../utils/test/renderAppInsideRoutingWithStartingUrl';
import { SceneObject } from '../../core/types';
import { EmbeddedScene } from '../EmbeddedScene';
import { SceneFlexItem, SceneFlexLayout } from '../layout/SceneFlexLayout';
Expand All @@ -12,8 +12,12 @@ import { SceneAppPage } from './SceneAppPage';
import { SceneRouteMatch } from './types';
import { SceneReactObject } from '../SceneReactObject';

// Mock lives in ./__mocks__/@grafana/runtime.js
jest.mock('@grafana/runtime');
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
PluginPage: jest.fn().mockImplementation((props: PluginPageProps) => {
return <div>{props.children}</div>;
}),
}));
jest.mock('../../utils/utils', () => ({
...jest.requireActual('../../utils/utils'),
useLocationServiceSafe: () => locationService,
Expand Down

This file was deleted.

8 changes: 6 additions & 2 deletions packages/scenes/utils/setupTestsAfterEnv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import '@testing-library/jest-dom';

import { matchers } from './test';
import { toEmitValues } from './test/toEmitValues';
import { toEmitValuesWith } from './test/toEmitValuesWith';

expect.extend(matchers);
expect.extend({
toEmitValues,
toEmitValuesWith,
});
3 changes: 2 additions & 1 deletion packages/scenes/utils/test/TestContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { locationService, LocationServiceProvider } from '@grafana/runtime';
import React, { useEffect, useState } from 'react';
import { Router } from 'react-router-dom';
import { SceneObject, UrlSyncContextProvider } from '../../src';
import { SceneObject } from '../../src/core/types';
import { UrlSyncContextProvider } from '../../src/services/UrlSyncContextProvider';

const history = locationService.getHistory();

Expand Down
7 changes: 0 additions & 7 deletions packages/scenes/utils/test/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { locationService } from '@grafana/runtime';
import { render } from '@testing-library/react';
import { SceneApp } from '../../src/components/SceneApp/SceneApp';
import { TestContextProviderBase } from './TestContextProvider';

export function renderAppInsideRouterWithStartingUrl(app: SceneApp, startingUrl: string) {
locationService.push(startingUrl);

return render(
<TestContextProviderBase>
<app.Component model={app} />
</TestContextProviderBase>
);
}
2 changes: 1 addition & 1 deletion packages/scenes/utils/test/updateUrlStateAndSyncState.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UrlQueryMap } from '@grafana/data';
import { locationService } from '@grafana/runtime';
import { UrlSyncManager } from '../../src';
import { UrlSyncManager } from '../../src/services/UrlSyncManager';

export function updateUrlStateAndSyncState(searchParams: UrlQueryMap, urlManager: UrlSyncManager) {
locationService.partial(searchParams);
Expand Down
17 changes: 1 addition & 16 deletions packages/scenes/utils/test/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import React from 'react';
import { locationService } from '@grafana/runtime';
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
import { asapScheduler, Subscription, timer, isObservable } from 'rxjs';
import { render } from '@testing-library/react';
import { SceneApp } from '../../src/components/SceneApp/SceneApp';
import { SceneObject, SceneObjectState } from '../../src';
import { TestContextProviderBase } from './TestContextProvider';
import { SceneObject, SceneObjectState } from '../../src/core/types';

export const OBSERVABLE_TEST_TIMEOUT_IN_MS = 1000;

Expand Down Expand Up @@ -68,16 +63,6 @@ export function expectObservable(received: unknown): jest.CustomMatcherResult |
return null;
}

export function renderAppInsideRouterWithStartingUrl(app: SceneApp, startingUrl: string) {
locationService.push(startingUrl);

return render(
<TestContextProviderBase>
<app.Component model={app} />
</TestContextProviderBase>
);
}

export function subscribeToStateUpdates<T extends SceneObjectState>(obj: SceneObject<T>): T[] {
const stateUpdates: T[] = [];
obj.subscribeToState((state) => stateUpdates.push(state));
Expand Down

0 comments on commit 8ee5f00

Please sign in to comment.