Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bd039e

Browse files
committed
feat(site): add ActionCell component
Summary: This is a direct follow-up to #484 and a next step in porting over the AuditLog with appropriate refactoring. This isn't a direct port; we used to have a map of icons on the FE, but we want to no loner split the logic between FE and BE. If we want icons at a later time point, we can return an icon identifier in the response, or we can simplify the view and gain real estate by omitting the icons. At the time of this commit, I prefer omitting the icons. Details: - Port over ActionCell from v1, sans icons - Add tests and stories for ActionCell Impact: This change does not have any user-facing impact yet, because the ActionCell is not yet rendered in the product. This enables an incremental approach to migrating in the FE of the Audit Log, which is still waiting on the BE port. Relations: - This commit relates to #472, but does not finish it. - This commit should not merged until after #484 because it's based from it
1 parent f006d0b commit 7bd039e

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ComponentMeta, Story } from "@storybook/react"
2+
import React from "react"
3+
import { ActionCell, ActionCellProps } from "./ActionCell"
4+
5+
export default {
6+
title: "AuditLog/Cells/ActionCell",
7+
component: ActionCell,
8+
} as ComponentMeta<typeof ActionCell>
9+
10+
const Template: Story<ActionCellProps> = (args) => <ActionCell {...args} />
11+
12+
export const Example = Template.bind({})
13+
Example.args = {
14+
action: "create",
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ActionCell, ActionCellProps } from "./ActionCell"
2+
import React from "react"
3+
import { render, screen } from "@testing-library/react"
4+
5+
namespace Helpers {
6+
export const Component: React.FC<ActionCellProps> = (props) => <ActionCell {...props} />
7+
}
8+
9+
describe("ActionCellProps", () => {
10+
it.each<[ActionCellProps, boolean]>([
11+
[{ action: "Create" }, false],
12+
[{ action: "" }, true],
13+
])(`validate(%p) throws: %p`, (props, throws) => {
14+
const validate = () => {
15+
ActionCellProps.validate(props)
16+
}
17+
18+
if (throws) {
19+
expect(validate).toThrowError()
20+
} else {
21+
expect(validate).not.toThrowError()
22+
}
23+
})
24+
})
25+
26+
describe("ActionCell", () => {
27+
it("renders the action", () => {
28+
// Given
29+
const props: ActionCellProps = {
30+
action: "Create",
31+
}
32+
33+
// When
34+
render(<Helpers.Component {...props} />)
35+
36+
// Then
37+
expect(screen.getByText(props.action)).toBeDefined()
38+
})
39+
40+
it("throws when action is an empty string", () => {
41+
// Given
42+
const props: ActionCellProps = {
43+
action: "",
44+
}
45+
46+
// When
47+
const shouldThrow = () => {
48+
render(<Helpers.Component {...props} />)
49+
}
50+
51+
// Then
52+
expect(shouldThrow).toThrowError()
53+
})
54+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Box from "@material-ui/core/Box"
2+
import Typography from "@material-ui/core/Typography"
3+
import React from "react"
4+
5+
export interface ActionCellProps {
6+
action: string
7+
}
8+
export namespace ActionCellProps {
9+
/**
10+
* validate that the received props are valid
11+
*
12+
* @throws Error if invalid
13+
*/
14+
export const validate = (props: ActionCellProps): void => {
15+
if (!props.action.trim()) {
16+
throw new Error(`invalid action: '${props.action}'`)
17+
}
18+
}
19+
}
20+
21+
export const ActionCell: React.FC<ActionCellProps> = ({ action }) => {
22+
ActionCellProps.validate({ action })
23+
24+
return (
25+
<Box display="flex" alignItems="center">
26+
<Typography color="textSecondary" variant="caption">
27+
{action}
28+
</Typography>
29+
</Box>
30+
)
31+
}

0 commit comments

Comments
 (0)