test: cover use cookie auth provider

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-03-13 12:20:47 +01:00
parent e069035158
commit 756ddb637f
3 changed files with 144 additions and 18 deletions
+19 -18
View File
@@ -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"
}
}
@@ -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<AuthApi>({ id: 'auth-test' });
const apiMock = {
getCookie: jest.fn().mockReturnValue(new Promise(() => {})),
};
await renderInTestApp(
<TestApiProvider apis={[[apiRef, apiMock]]}>
<CookieAuthRefreshProvider apiRef={apiRef}>
<div>Test Content</div>
</CookieAuthRefreshProvider>
</TestApiProvider>,
);
expect(screen.getByTestId('progress')).toBeInTheDocument();
});
it('should render a error panel', async () => {
const apiRef = createApiRef<AuthApi>({ id: 'auth-test' });
const error = new Error('Failed to get cookie');
const apiMock = {
getCookie: jest.fn().mockRejectedValue(error),
};
await renderInTestApp(
<TestApiProvider apis={[[apiRef, apiMock]]}>
<CookieAuthRefreshProvider apiRef={apiRef}>
<div>Test Content</div>
</CookieAuthRefreshProvider>
</TestApiProvider>,
);
expect(screen.getByText(error.message)).toBeInTheDocument();
});
it('should call the api again when retry is clicked', async () => {
const apiRef = createApiRef<AuthApi>({ id: 'auth-test' });
const error = new Error('Failed to get cookie');
const apiMock = {
getCookie: jest.fn().mockRejectedValueOnce(error).mockResolvedValue({
expiresAt: getExpiresAtInFuture(),
}),
};
await renderInTestApp(
<TestApiProvider apis={[[apiRef, apiMock]]}>
<CookieAuthRefreshProvider apiRef={apiRef}>
<div>Test Content</div>
</CookieAuthRefreshProvider>
</TestApiProvider>,
);
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<AuthApi>({ id: 'auth-test' });
const apiMock = {
getCookie: jest.fn().mockResolvedValue({
expiresAt: {
expiresAt: getExpiresAtInFuture(),
},
}),
};
await renderInTestApp(
<TestApiProvider apis={[[apiRef, apiMock]]}>
<CookieAuthRefreshProvider apiRef={apiRef}>
<div>Test Content</div>
</CookieAuthRefreshProvider>
</TestApiProvider>,
);
await waitFor(() =>
expect(screen.getByText('Test Content')).toBeInTheDocument(),
);
});
});