From a39f0929266daf53f1051bb3d45d9b1cda2ef5ad Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 25 Apr 2022 13:25:18 +0200 Subject: [PATCH] feat: footer & keyboardkeys (#86) * Add Footer and KeyboardKeys components * Fix lint issues * Fix typo --- .../__tests__/mocks/mockTauriIPC.ts | 70 ++++++++++++++++ applications/launchpad_v2/src/App.test.tsx | 24 +++--- .../src/components/Footer/Footer.test.tsx | 59 ++++++++++++- .../src/components/Footer/index.tsx | 84 ++++++++++++++++++- .../src/components/Footer/styles.ts | 4 +- .../KeyboardKeys/KeyboardKeys.test.tsx | 18 ++++ .../src/components/KeyboardKeys/index.tsx | 40 +++++++++ .../src/components/KeyboardKeys/styles.ts | 31 +++++++ .../src/components/KeyboardKeys/types.ts | 3 + .../src/components/Text/index.tsx | 14 +++- .../launchpad_v2/src/components/Text/types.ts | 2 + .../src/components/TitleBar/index.tsx | 2 +- .../src/layouts/MainLayout/styles.ts | 5 +- .../launchpad_v2/src/locales/footer.ts | 7 ++ .../launchpad_v2/src/locales/index.ts | 2 + .../launchpad_v2/src/styles/Icons/CmdKey.tsx | 20 +++++ .../launchpad_v2/src/styles/Icons/WinKey.tsx | 33 ++++++++ .../launchpad_v2/src/styles/styles/colors.ts | 5 +- .../launchpad_v2/src/styles/themes/dark.ts | 14 ++-- .../launchpad_v2/src/styles/themes/light.ts | 13 +-- applications/launchpad_v2/yarn.lock | 5 ++ 21 files changed, 420 insertions(+), 35 deletions(-) create mode 100644 applications/launchpad_v2/__tests__/mocks/mockTauriIPC.ts create mode 100644 applications/launchpad_v2/src/components/KeyboardKeys/KeyboardKeys.test.tsx create mode 100644 applications/launchpad_v2/src/components/KeyboardKeys/index.tsx create mode 100644 applications/launchpad_v2/src/components/KeyboardKeys/styles.ts create mode 100644 applications/launchpad_v2/src/components/KeyboardKeys/types.ts create mode 100644 applications/launchpad_v2/src/locales/footer.ts create mode 100644 applications/launchpad_v2/src/styles/Icons/CmdKey.tsx create mode 100644 applications/launchpad_v2/src/styles/Icons/WinKey.tsx diff --git a/applications/launchpad_v2/__tests__/mocks/mockTauriIPC.ts b/applications/launchpad_v2/__tests__/mocks/mockTauriIPC.ts new file mode 100644 index 0000000000..b643131460 --- /dev/null +++ b/applications/launchpad_v2/__tests__/mocks/mockTauriIPC.ts @@ -0,0 +1,70 @@ +import { mockIPC } from '@tauri-apps/api/mocks' + +/** + * Set of default values returned by `tauriIPCMock()` + */ +export const defaultTauriMockValues: Record = { + os: { + arch: 'x86_64', + platform: 'darwin', + ostype: 'Darwin', + }, +} + +/** + * The Tauri IPC mock. + * + * It uses Tauri's mockIPC and returns the value set in the `props`. + * If nothing found in `props`, it will return a value from `defaultTauriMockValues`. + * + * @param {Record} props - pass the value you expect in tests + * + * @example + * // Use default values: + * tauriIPCMock() + * + * // Get given value from specific API module (ie. 'platform' from 'os' module) + * tauriIPCMock({ + * os: { + * platform: 'darwin', + * }, + * }) + */ +export const tauriIPCMock = (props: Record = undefined) => { + return mockIPC((cmd, args) => { + switch (cmd) { + case 'tauri': + return tauriCmdMock(cmd, args, props) + case 'invoke': + return + default: + return + } + }) +} + +const tauriCmdMock = ( + cmd: string, + args: Record, + props: Record, +) => { + const tauriModule = (args?.__tauriModule as string)?.toLowerCase() + const messageCmd = (args?.message as { cmd?: string })?.cmd?.toLowerCase() + + if (tauriModule && messageCmd) { + if ( + props && + Object.keys(props).includes(tauriModule) && + Object.keys(props[tauriModule]).includes(messageCmd) + ) { + return props[tauriModule][messageCmd] + } else if ( + Object.keys(defaultTauriMockValues).includes(tauriModule) && + Object.keys(defaultTauriMockValues[tauriModule]).includes(messageCmd) + ) { + return defaultTauriMockValues[tauriModule][messageCmd] + } + } + + return +} diff --git a/applications/launchpad_v2/src/App.test.tsx b/applications/launchpad_v2/src/App.test.tsx index 75b2281bb0..89a6155347 100644 --- a/applications/launchpad_v2/src/App.test.tsx +++ b/applications/launchpad_v2/src/App.test.tsx @@ -1,11 +1,12 @@ -import React from 'react' -import { render } from '@testing-library/react' +import { act, render } from '@testing-library/react' import { randomFillSync } from 'crypto' import { clearMocks } from '@tauri-apps/api/mocks' import { ThemeProvider } from 'styled-components' +import { Provider } from 'react-redux' import App from './App' -import { Provider } from 'react-redux' + +import { tauriIPCMock } from '../__tests__/mocks/mockTauriIPC' import { store } from './store' import themes from './styles/themes' @@ -23,11 +24,14 @@ afterEach(() => { }) test('renders without crashing', async () => { - render( - - - - - , - ) + tauriIPCMock() + await act(async () => { + render( + + + + + , + ) + }) }) diff --git a/applications/launchpad_v2/src/components/Footer/Footer.test.tsx b/applications/launchpad_v2/src/components/Footer/Footer.test.tsx index 8a36eb3333..66804a85dd 100644 --- a/applications/launchpad_v2/src/components/Footer/Footer.test.tsx +++ b/applications/launchpad_v2/src/components/Footer/Footer.test.tsx @@ -1,12 +1,65 @@ -import { render, screen } from '@testing-library/react' +import { clearMocks } from '@tauri-apps/api/mocks' +import { act, render, screen } from '@testing-library/react' +import { randomFillSync } from 'crypto' import Footer from '.' +import { tauriIPCMock } from '../../../__tests__/mocks/mockTauriIPC' + +beforeAll(() => { + window.crypto = { + getRandomValues: function (buffer) { + return randomFillSync(buffer) + }, + } +}) + +afterEach(() => { + clearMocks() +}) describe('Footer', () => { - it('should render without crash', () => { - render(