Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsomsanith committed Apr 17, 2022
1 parent 7d836c9 commit 1c33d26
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions addons/a11y/src/manager.test.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { addons } from '@storybook/addons';
import * as api from '@storybook/api';
import { ADDON_ID } from './constants';
import { PANEL_ID } from './constants';
import './manager';

jest.mock('@storybook/api');
jest.mock('@storybook/addons');
const mockedApi = api as jest.Mocked<typeof api>;
const mockedApi = api as unknown as jest.Mocked<api.API>;
mockedApi.getAddonState = jest.fn();
const mockedAddons = addons as jest.Mocked<typeof addons>;
const registrationImpl = mockedAddons.register.mock.calls[0][1];

describe('A11yManager', () => {
it('should register the panels', () => {
// when
registrationImpl(mockedApi as unknown as api.API);
registrationImpl(mockedApi);

// then
expect(mockedAddons.add.mock.calls).toHaveLength(2);
expect(mockedAddons.add).toHaveBeenCalledWith(ADDON_ID, expect.anything());
expect(mockedAddons.add).toHaveBeenCalledWith(PANEL_ID, expect.anything());

const panel = mockedAddons.add.mock.calls
.map(([_, def]) => def)
Expand All @@ -30,8 +31,8 @@ describe('A11yManager', () => {

it('should compute title with no issues', () => {
// given
mockedApi.useAddonState.mockImplementation((_, defaultState) => [defaultState, jest.fn()]);
registrationImpl(mockedApi as unknown as api.API);
mockedApi.getAddonState.mockImplementation(() => undefined);
registrationImpl(api as unknown as api.API);
const title = mockedAddons.add.mock.calls
.map(([_, def]) => def)
.find(({ type }) => type === 'panel').title as Function;
Expand All @@ -42,11 +43,8 @@ describe('A11yManager', () => {

it('should compute title with issues', () => {
// given
mockedApi.useAddonState.mockImplementation(() => [
{ violations: [{}], incomplete: [{}, {}] },
jest.fn(),
]);
registrationImpl(mockedApi as unknown as api.API);
mockedApi.getAddonState.mockImplementation(() => ({ violations: [{}], incomplete: [{}, {}] }));
registrationImpl(mockedApi);
const title = mockedAddons.add.mock.calls
.map(([_, def]) => def)
.find(({ type }) => type === 'panel').title as Function;
Expand Down
2 changes: 1 addition & 1 deletion addons/a11y/src/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addons.register(ADDON_ID, (api) => {

addons.add(PANEL_ID, {
title() {
const addonState = api?.getAddonState<Results>(ADDON_ID);
const addonState: Results = api?.getAddonState(ADDON_ID);
const violationsNb = addonState?.violations?.length || 0;
const incompleteNb = addonState?.incomplete?.length || 0;
const totalNb = violationsNb + incompleteNb;
Expand Down

0 comments on commit 1c33d26

Please sign in to comment.