1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

Fix: add tests

This commit is contained in:
Goktug Poyraz 2023-03-20 14:28:46 +01:00
parent d2b44a338d
commit c1b607bdba
3 changed files with 181 additions and 3 deletions

View File

@ -121,7 +121,7 @@ export async function getErrorHtml(
}
///: BEGIN:ONLY_INCLUDE_IN(flask)
const MMD_DOWNLOAD_LINK =
export const MMD_DOWNLOAD_LINK =
'https://github.com/MetaMask/metamask-desktop/releases';
function disableDesktop(backgroundConnection) {

View File

@ -1,13 +1,48 @@
import browser from 'webextension-polyfill';
import { fetchLocale } from '../../ui/helpers/utils/i18n-helper';
import { SUPPORT_LINK } from './ui-utils';
import { getErrorHtml } from './error-utils';
import {
downloadDesktopApp,
openOrDownloadMMD,
downloadExtension,
getErrorHtml,
restartExtension,
registerDesktopErrorActions,
MMD_DOWNLOAD_LINK,
} from './error-utils';
import { openCustomProtocol } from './deep-linking';
jest.mock('../../ui/helpers/utils/i18n-helper', () => ({
fetchLocale: jest.fn(),
loadRelativeTimeFormatLocaleData: jest.fn(),
}));
jest.mock('./deep-linking', () => ({
openCustomProtocol: jest.fn(),
}));
jest.mock('webextension-polyfill', () => {
return {
runtime: {
reload: jest.fn(),
},
};
});
describe('Error utils Tests', function () {
beforeEach(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
global.platform = {
openTab: jest.fn(),
};
});
afterAll(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
delete global.platform;
});
it('should get error html', async function () {
const mockStore = {
localeMessages: {
@ -50,4 +85,93 @@ describe('Error utils Tests', function () {
expect(errorHtml).toContain(stillGettingMessageMessage);
expect(errorHtml).toContain(sendBugReportMessage);
});
describe('desktop', () => {
it('downloadDesktopApp opens a new tab on metamask-desktop releases url', () => {
downloadDesktopApp();
expect(global.platform.openTab).toHaveBeenCalledTimes(1);
expect(global.platform.openTab).toHaveBeenCalledWith({
url: MMD_DOWNLOAD_LINK,
});
});
it('downloadExtension opens a new tab on metamask extension url', () => {
downloadExtension();
expect(global.platform.openTab).toHaveBeenCalledTimes(1);
expect(global.platform.openTab).toHaveBeenCalledWith({
url: 'https://metamask.io/',
});
});
it('restartExtension calls runtime reload method', () => {
restartExtension();
expect(browser.runtime.reload).toHaveBeenCalledTimes(1);
});
describe('openOrDownloadMMD', () => {
it('launches installed desktop app by calling openCustomProtocol successfully', () => {
openCustomProtocol.mockResolvedValue();
openOrDownloadMMD();
expect(openCustomProtocol).toHaveBeenCalledTimes(1);
expect(openCustomProtocol).toHaveBeenCalledWith(
'metamask-desktop://pair',
);
});
it('opens metamask-desktop release url when fails to find and start a local metamask-desktop app', async () => {
openCustomProtocol.mockRejectedValue();
const focusMock = jest.fn();
jest.spyOn(window, 'open').mockReturnValue({
focus: focusMock,
});
openOrDownloadMMD();
// this ensures that we are awaiting for pending promises to resolve
// as the openOrDownloadMMD calls a promise, but returns before it is resolved
await new Promise(process.nextTick);
expect(openCustomProtocol).toHaveBeenCalledTimes(1);
expect(openCustomProtocol).toHaveBeenCalledWith(
'metamask-desktop://pair',
);
expect(window.open).toHaveBeenCalledTimes(1);
expect(window.open).toHaveBeenCalledWith(MMD_DOWNLOAD_LINK, '_blank');
expect(focusMock).toHaveBeenCalledTimes(1);
});
});
it('registerDesktopErrorActions add click event listeners for each desktop error elements', async () => {
const addEventListenerMock = jest.fn();
jest.spyOn(document, 'getElementById').mockReturnValue({
addEventListener: addEventListenerMock,
});
registerDesktopErrorActions();
expect(document.getElementById).toHaveBeenCalledTimes(4);
expect(document.getElementById).toHaveBeenNthCalledWith(
1,
'desktop-error-button-disable-mmd',
);
expect(document.getElementById).toHaveBeenNthCalledWith(
2,
'desktop-error-button-restart-mm',
);
expect(document.getElementById).toHaveBeenNthCalledWith(
3,
'desktop-error-button-download-mmd',
);
expect(document.getElementById).toHaveBeenNthCalledWith(
4,
'desktop-error-button-open-or-download-mmd',
);
expect(addEventListenerMock).toHaveBeenCalledTimes(4);
});
});
});

View File

@ -1,11 +1,12 @@
import React from 'react';
import reactRouterDom from 'react-router-dom';
import { waitFor, act, screen } from '@testing-library/react';
import { waitFor, act, screen, fireEvent } from '@testing-library/react';
import actions from '../../store/actions';
import configureStore from '../../store/store';
import { renderWithProvider } from '../../../test/jest';
import mockState from '../../../test/data/mock-state.json';
import { SECOND } from '../../../shared/constants/time';
import { useCopyToClipboard } from '../../hooks/useCopyToClipboard';
import DesktopPairingPage from '.';
const mockHideLoadingIndication = jest.fn();
@ -19,10 +20,13 @@ jest.mock('../../store/actions', () => {
};
});
jest.mock('../../hooks/useCopyToClipboard');
const mockedActions = actions;
describe('Desktop Pairing page', () => {
const mockHistoryPush = jest.fn();
const handleCopy = jest.fn();
function flushPromises() {
// Wait for promises running in the non-async timer callback to complete.
@ -35,6 +39,7 @@ describe('Desktop Pairing page', () => {
.spyOn(reactRouterDom, 'useHistory')
.mockImplementation()
.mockReturnValue({ push: mockHistoryPush });
useCopyToClipboard.mockReturnValue([false, handleCopy]);
});
afterEach(() => {
@ -107,4 +112,53 @@ describe('Desktop Pairing page', () => {
jest.clearAllTimers();
jest.useRealTimers();
});
it('should copy otp value when content is clicked', async () => {
const otp = '123456';
mockedActions.generateDesktopOtp.mockResolvedValue(otp);
const store = configureStore(mockState);
act(() => {
renderWithProvider(<DesktopPairingPage />, store);
});
await waitFor(() => {
expect(screen.getByTestId('desktop-pairing-otp-content')).toBeDefined();
expect(screen.getByText(otp)).toBeDefined();
});
act(() => {
fireEvent.click(screen.getByTestId('desktop-pairing-otp-content'));
});
await waitFor(() => {
expect(handleCopy).toHaveBeenCalledWith(otp);
});
});
it('should return to previews page when the done button is clicked', async () => {
const otp = '123456';
const mostRecentOverviewPage = '/mostRecentOverviewPage';
mockedActions.generateDesktopOtp.mockResolvedValue(otp);
const store = configureStore(mockState);
act(() => {
renderWithProvider(<DesktopPairingPage />, store);
});
await waitFor(() => {
expect(screen.getByTestId('desktop-pairing-otp-content')).toBeDefined();
});
act(() => {
fireEvent.click(screen.getByText('Done'));
});
await waitFor(() => {
expect(mockHistoryPush).toHaveBeenCalledTimes(1);
expect(mockHistoryPush).toHaveBeenCalledWith(mostRecentOverviewPage);
});
});
});