diff --git a/plugins/auth-react/package.json b/plugins/auth-react/package.json index 089dafd7f6..fbe44d91d4 100644 --- a/plugins/auth-react/package.json +++ b/plugins/auth-react/package.json @@ -1,27 +1,30 @@ { "name": "@backstage/plugin-auth-react", - "description": "Web library for the auth plugin", "version": "0.0.0", - "main": "src/index.ts", - "types": "src/index.ts", - "license": "Apache-2.0", + "description": "Web library for the auth plugin", + "backstage": { + "role": "web-library" + }, "publishConfig": { "access": "public", "main": "dist/index.esm.js", "types": "dist/index.d.ts" }, - "backstage": { - "role": "web-library" - }, + "license": "Apache-2.0", "sideEffects": false, + "main": "src/index.ts", + "types": "src/index.ts", + "files": [ + "dist" + ], "scripts": { - "start": "backstage-cli package start", "build": "backstage-cli package build", - "lint": "backstage-cli package lint", - "test": "backstage-cli package test", "clean": "backstage-cli package clean", + "lint": "backstage-cli package lint", "prepack": "backstage-cli package prepack", - "postpack": "backstage-cli package postpack" + "postpack": "backstage-cli package postpack", + "start": "backstage-cli package start", + "test": "backstage-cli package test" }, "dependencies": { "@backstage/core-components": "workspace:^", @@ -29,16 +32,14 @@ "@material-ui/core": "^4.9.13", "@react-hookz/web": "^24.0.4" }, - "peerDependencies": { - "react": "^16.13.1 || ^17.0.0 || ^18.0.0" - }, "devDependencies": { "@backstage/cli": "workspace:^", "@backstage/test-utils": "workspace:^", "@testing-library/jest-dom": "^6.0.0", - "@testing-library/react": "^14.0.0" + "@testing-library/react": "^14.0.0", + "@testing-library/user-event": "^14.0.0" }, - "files": [ - "dist" - ] + "peerDependencies": { + "react": "^16.13.1 || ^17.0.0 || ^18.0.0" + } } diff --git a/plugins/auth-react/src/components/CookieAuthRefreshProvider/CookieAuthRefreshProvider.test.tsx b/plugins/auth-react/src/components/CookieAuthRefreshProvider/CookieAuthRefreshProvider.test.tsx new file mode 100644 index 0000000000..6684fa7856 --- /dev/null +++ b/plugins/auth-react/src/components/CookieAuthRefreshProvider/CookieAuthRefreshProvider.test.tsx @@ -0,0 +1,124 @@ +/* + * Copyright 2024 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 { screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { CookieAuthRefreshProvider } from './CookieAuthRefreshProvider'; +import { TestApiProvider, renderInTestApp } from '@backstage/test-utils'; +import { createApiRef } from '@backstage/core-plugin-api'; +import { AuthApi } from '../../types'; + +describe('CookieAuthRefreshProvider', () => { + function getExpiresAtInFuture() { + const tenMinutesInMilliseconds = 10 * 60 * 1000; + return new Date(Date.now() + tenMinutesInMilliseconds).toISOString(); + } + + global.BroadcastChannel = jest.fn().mockImplementation(() => ({ + postMessage: jest.fn(), + addEventListener: jest.fn(), + removeEventListener: jest.fn(), + })); + + it('should render a progress bar', async () => { + const apiRef = createApiRef({ id: 'auth-test' }); + const apiMock = { + getCookie: jest.fn().mockReturnValue(new Promise(() => {})), + }; + + await renderInTestApp( + + +
Test Content
+
+
, + ); + + expect(screen.getByTestId('progress')).toBeInTheDocument(); + }); + + it('should render a error panel', async () => { + const apiRef = createApiRef({ id: 'auth-test' }); + const error = new Error('Failed to get cookie'); + const apiMock = { + getCookie: jest.fn().mockRejectedValue(error), + }; + + await renderInTestApp( + + +
Test Content
+
+
, + ); + + expect(screen.getByText(error.message)).toBeInTheDocument(); + }); + + it('should call the api again when retry is clicked', async () => { + const apiRef = createApiRef({ id: 'auth-test' }); + const error = new Error('Failed to get cookie'); + const apiMock = { + getCookie: jest.fn().mockRejectedValueOnce(error).mockResolvedValue({ + expiresAt: getExpiresAtInFuture(), + }), + }; + + await renderInTestApp( + + +
Test Content
+
+
, + ); + + expect(apiMock.getCookie).toHaveBeenCalledTimes(1); + + expect(screen.getByText(error.message)).toBeInTheDocument(); + + await userEvent.click(screen.getByText('Retry')); + + expect(apiMock.getCookie).toHaveBeenCalledTimes(2); + + await waitFor(() => + expect(screen.getByText('Test Content')).toBeInTheDocument(), + ); + }); + + it('should render the children', async () => { + const apiRef = createApiRef({ id: 'auth-test' }); + const apiMock = { + getCookie: jest.fn().mockResolvedValue({ + expiresAt: { + expiresAt: getExpiresAtInFuture(), + }, + }), + }; + + await renderInTestApp( + + +
Test Content
+
+
, + ); + + await waitFor(() => + expect(screen.getByText('Test Content')).toBeInTheDocument(), + ); + }); +}); diff --git a/yarn.lock b/yarn.lock index c438e5f39d..3a8010a02c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5122,6 +5122,7 @@ __metadata: "@react-hookz/web": ^24.0.4 "@testing-library/jest-dom": ^6.0.0 "@testing-library/react": ^14.0.0 + "@testing-library/user-event": ^14.0.0 peerDependencies: react: ^16.13.1 || ^17.0.0 || ^18.0.0 languageName: unknown