core-components: add test for ProxiedSignInPage

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-01-10 19:27:34 +01:00
parent f56b7290da
commit bb4089890b
6 changed files with 119 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/test-utils': minor
---
Added `components` option to `TestAppOptions`, which will be forwarded as the `components` option to `createApp`.
+1
View File
@@ -92,6 +92,7 @@
"react-router-dom": "6.0.0-beta.0 || ^6.3.0"
},
"devDependencies": {
"@backstage/app-defaults": "workspace:^",
"@backstage/cli": "workspace:^",
"@backstage/core-app-api": "workspace:^",
"@backstage/test-utils": "workspace:^",
@@ -0,0 +1,103 @@
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { render, screen } from '@testing-library/react';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import {
TestApiProvider,
setupRequestMockHandlers,
wrapInTestApp,
} from '@backstage/test-utils';
import { ProxiedSignInPage } from './ProxiedSignInPage';
import { discoveryApiRef } from '@backstage/core-plugin-api';
describe('ProxiedSignInPage', () => {
const worker = setupServer();
setupRequestMockHandlers(worker);
const Subject = wrapInTestApp(<div>authenticated</div>, {
components: {
SignInPage: props => (
<TestApiProvider
apis={[
[
discoveryApiRef,
{
getBaseUrl: async () => 'http://example.com/api/auth',
},
],
]}
>
<ProxiedSignInPage {...props} provider="test" />
</TestApiProvider>
),
},
});
it('should sign in a user', async () => {
worker.use(
rest.get('http://example.com/api/auth/test/refresh', (_, res, ctx) =>
res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
profile: {
email: 'e',
displayName: 'd',
picture: 'p',
},
backstageIdentity: {
token: 'a.e30.c',
identity: {
type: 'user',
userEntityRef: 'k:ns/ue',
ownershipEntityRefs: ['k:ns/oe'],
},
},
}),
),
),
);
render(Subject);
await expect(
screen.findByText('authenticated'),
).resolves.toBeInTheDocument();
});
it('should forward error', async () => {
worker.use(
rest.get('http://example.com/api/auth/test/refresh', (_, res, ctx) =>
res(
ctx.status(401),
ctx.set('Content-Type', 'application/json'),
ctx.json({
error: { name: 'Error', message: 'not-displayed' },
}),
),
),
);
render(Subject);
await expect(
screen.findByText('Request failed with 401 Error'),
).resolves.toBeInTheDocument();
});
});
+2
View File
@@ -7,6 +7,7 @@ import { AnalyticsApi } from '@backstage/core-plugin-api';
import { AnalyticsEvent } from '@backstage/core-plugin-api';
import { ApiHolder } from '@backstage/core-plugin-api';
import { ApiRef } from '@backstage/core-plugin-api';
import { AppComponents } from '@backstage/core-plugin-api';
import { AuthorizeResult } from '@backstage/plugin-permission-common';
import { ComponentType } from 'react';
import { Config } from '@backstage/config';
@@ -243,6 +244,7 @@ export type TestAppOptions = {
mountedRoutes?: {
[path: string]: RouteRef | ExternalRouteRef;
};
components?: Partial<AppComponents>;
};
// @public
@@ -25,6 +25,7 @@ import { themes, UnifiedThemeProvider } from '@backstage/theme';
import MockIcon from '@material-ui/icons/AcUnit';
import { createSpecializedApp } from '@backstage/core-app-api';
import {
AppComponents,
attachComponentData,
BootErrorPageProps,
createRouteRef,
@@ -101,6 +102,11 @@ export type TestAppOptions = {
* const link = useRouteRef(myRouteRef)
*/
mountedRoutes?: { [path: string]: RouteRef | ExternalRouteRef };
/**
* Components to be forwarded to the `components` option of `createApp`.
*/
components?: Partial<AppComponents>;
};
function isExternalRouteRef(
@@ -137,6 +143,7 @@ export function createTestAppWrapper(
Router: ({ children }) => (
<MemoryRouter initialEntries={routeEntries} children={children} />
),
...options.components,
},
icons: mockIcons,
plugins: [],
+1
View File
@@ -3921,6 +3921,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@backstage/core-components@workspace:packages/core-components"
dependencies:
"@backstage/app-defaults": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
"@backstage/core-app-api": "workspace:^"