forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Footer and KeyboardKeys components
- Loading branch information
1 parent
62fd8f8
commit c0d550f
Showing
21 changed files
with
409 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { mockIPC } from '@tauri-apps/api/mocks' | ||
|
||
/** | ||
* Set of default values returned by `tauriIPCMock()` | ||
*/ | ||
export const defaultTauriMockValues: Record<string, unknown> = { | ||
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 from `defaultTauriMockValues`. | ||
* | ||
* @param {Record<string, unknown>} 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<string, unknown> = 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<string, unknown>, props: Record<string, unknown>) => { | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 56 additions & 3 deletions
59
applications/launchpad_v2/src/components/Footer/Footer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(<Footer />) | ||
it('should render without crashing', async () => { | ||
tauriIPCMock() | ||
|
||
await act(async () => { | ||
render(<Footer />) | ||
}) | ||
|
||
const el = screen.getByTestId('footer-cmp') | ||
expect(el).toBeInTheDocument() | ||
}) | ||
|
||
it('should render text for supported OS type', async () => { | ||
tauriIPCMock({ | ||
os: { | ||
arch: 'x86_64', | ||
platform: 'darwin', | ||
ostype: 'Darwin', | ||
}, | ||
}) | ||
|
||
await act(async () => { | ||
render(<Footer />) | ||
}) | ||
|
||
const el = screen.getByTestId('terminal-instructions-in-footer') | ||
expect(el).toBeInTheDocument() | ||
}) | ||
|
||
it('should NOT render any instructions if met unsupported OS type', async () => { | ||
tauriIPCMock({ | ||
os: { | ||
arch: 'x86_64', | ||
platform: 'darwin', | ||
ostype: 'unsupported', | ||
}, | ||
}) | ||
|
||
await act(async () => { | ||
render(<Footer />) | ||
}) | ||
|
||
const el = screen.queryByTestId('terminal-instructions-in-footer') | ||
expect(el).not.toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
applications/launchpad_v2/src/components/KeyboardKeys/KeyboardKeys.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { render, screen } from '@testing-library/react' | ||
|
||
import KeyboardKeys from '.' | ||
|
||
describe('KeyboardKeys', () => { | ||
it('should render given keys', async () => { | ||
render(<KeyboardKeys keys={['Ctrl', 'R', 'win']} />) | ||
|
||
const ctrlTile = screen.getByText('Ctrl') | ||
expect(ctrlTile).toBeInTheDocument() | ||
|
||
const rTile = screen.getByText('R') | ||
expect(rTile).toBeInTheDocument() | ||
|
||
const winTile = screen.getByTestId('svg-win-key') | ||
expect(winTile).toBeInTheDocument() | ||
}) | ||
}) |
40 changes: 40 additions & 0 deletions
40
applications/launchpad_v2/src/components/KeyboardKeys/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { IconsWrapper, KeyTile, LetterKey } from './styles' | ||
import { KeyboardKeysProps } from './types' | ||
import SvgCmdKey from '../../styles/Icons/CmdKey' | ||
import SvgWinKey from '../../styles/Icons/WinKey' | ||
|
||
/** | ||
* Renders keyboard keys as set of tiles. | ||
* Use whenever you need to show the keyboard shortcuts, ie: "Ctrl + Alt + T" | ||
* | ||
* Use 'win' and 'cmd' to render Windows and Command keys. | ||
* | ||
* @param {string[]} keys - the set of keyboard keys | ||
* | ||
* @example | ||
* <KeyboardKeys keys={['Ctrl', 'Alt', 'win']} /> | ||
*/ | ||
const KeyboardKeys = ({ keys }: KeyboardKeysProps) => { | ||
const result = [] | ||
|
||
keys.forEach((key, idx) => { | ||
let symbol | ||
switch (key) { | ||
case 'win': | ||
symbol = <SvgWinKey /> | ||
break | ||
case 'cmd': | ||
symbol = <SvgCmdKey /> | ||
break | ||
default: | ||
symbol = <LetterKey>{key}</LetterKey> | ||
break | ||
} | ||
|
||
result.push(<KeyTile key={`keyboard-key-${idx}`}>{symbol}</KeyTile>) | ||
}) | ||
|
||
return <IconsWrapper>{result}</IconsWrapper> | ||
} | ||
|
||
export default KeyboardKeys |
31 changes: 31 additions & 0 deletions
31
applications/launchpad_v2/src/components/KeyboardKeys/styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import styled from 'styled-components' | ||
|
||
export const IconsWrapper = styled.div` | ||
display: inline-block; | ||
vertical-align: baseline; | ||
` | ||
|
||
export const KeyTile = styled.span` | ||
display: inline-block; | ||
vertical-align: middle; | ||
text-align: center; | ||
font-size: 10px; | ||
line-height: 10px; | ||
padding: 2px; | ||
background: transparent; | ||
border: 1px solid ${({ theme }) => theme.borderColorLight}; | ||
border-radius: 4px; | ||
min-width: 16px; | ||
height: 16px; | ||
box-sizing: border-box; | ||
margin-left: 1px; | ||
margin-right: 1px; | ||
margin-top: -4%; | ||
` | ||
|
||
export const LetterKey = styled.span` | ||
text-align: center; | ||
font-size: 12px; | ||
line-height: 12px; | ||
font-weight: 500; | ||
` |
3 changes: 3 additions & 0 deletions
3
applications/launchpad_v2/src/components/KeyboardKeys/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface KeyboardKeysProps { | ||
keys: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.