0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-31 22:51:25 -05:00

test(ui): add ut (#1136)

add ut
This commit is contained in:
simeng-li 2022-06-20 09:45:03 +08:00 committed by GitHub
parent 4df749e142
commit ea1df2d253
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 1 deletions

View file

@ -1,18 +1,34 @@
import { fireEvent } from '@testing-library/react';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import renderWithPageContext from '@/__mocks__/RenderWithPageContext';
import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider';
import { socialConnectors } from '@/__mocks__/logto';
import { ConnectorData } from '@/types';
import SocialSignInDropdown from '.';
const mockInvokeSocialSignIn = jest.fn();
// eslint-disable-next-line unicorn/consistent-function-scoping
jest.mock('@/hooks/use-social', () => () => ({
invokeSocialSignIn: (connector: ConnectorData) => {
mockInvokeSocialSignIn(connector);
},
}));
describe('SocialSignInDropdown', () => {
it('render properly', () => {
const { queryByText } = renderWithPageContext(
<SettingsProvider>
<MemoryRouter>
<SocialSignInDropdown isOpen connectors={socialConnectors} onClose={jest.fn} />
<SocialSignInDropdown
isOpen
connectors={socialConnectors}
anchorRef={{ current: document.createElement('div') }}
onClose={jest.fn}
/>
</MemoryRouter>
</SettingsProvider>
);
@ -21,4 +37,25 @@ describe('SocialSignInDropdown', () => {
expect(queryByText(name.en)).not.toBeNull();
}
});
it('invokeSignIn', () => {
const { getByText } = renderWithPageContext(
<SettingsProvider>
<MemoryRouter>
<SocialSignInDropdown
isOpen
connectors={socialConnectors}
anchorRef={{ current: document.createElement('div') }}
onClose={jest.fn}
/>
</MemoryRouter>
</SettingsProvider>
);
if (socialConnectors[0]?.name.en) {
const socialLink = getByText(socialConnectors[0]?.name.en);
fireEvent.click(socialLink);
expect(mockInvokeSocialSignIn).toBeCalledWith(socialConnectors[0]);
}
});
});

View file

@ -0,0 +1,36 @@
import { fireEvent } from '@testing-library/react';
import React from 'react';
import renderWithPageContext from '@/__mocks__/RenderWithPageContext';
import SettingsProvider from '@/__mocks__/RenderWithPageContext/SettingsProvider';
import TermsOfUsePromiseModal from '.';
describe('TermsOfUsePromiseModal', () => {
it('render properly', () => {
const onResolve = jest.fn();
const onReject = jest.fn();
const { getByText } = renderWithPageContext(
<SettingsProvider>
<TermsOfUsePromiseModal
isOpen
open
instanceId="1"
close={jest.fn()}
onReject={onReject}
onResolve={onResolve}
/>
</SettingsProvider>
);
const confirmButton = getByText('action.agree');
const cancelButton = getByText('action.cancel');
fireEvent.click(confirmButton);
expect(onResolve).toBeCalledWith(true);
fireEvent.click(cancelButton);
expect(onReject).toBeCalledWith(false);
});
});