Skip to content

Commit

Permalink
Feature/gra 995 workspace settings hide demo banners (#764)
Browse files Browse the repository at this point in the history
* Add sample_data to updateWorkspace

* Add workspace setting

* Fix mock

* Lint fixes

* Test fix
  • Loading branch information
edlouth authored Nov 1, 2023
1 parent 90bcf62 commit ab3bf85
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const DangerItem: React.FC<DangerItemProps> = ({
}) => (
<Card
variant="outlined"
sx={{ borderColor: theme => theme.palette.error.dark }}
sx={{ borderColor: theme => theme.palette.error.dark, mb: 2 }}
>
<List>
<ListItem
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import userEvent from "@testing-library/user-event"
import { GraphQLError } from "graphql"
import { act, render, screen, waitFor } from "testing"
import HideDemoWorkspace, { UPDATE_WORKSPACE } from "./HideDemoWorkspace"

const workspace = {
id: "1",
}

test("renders", async () => {
render(<HideDemoWorkspace workspace={workspace} />)
})

test("submit", async () => {
const user = userEvent.setup()

render(<HideDemoWorkspace workspace={workspace} />)

await act(async () => {
await user.click(screen.getByRole("button", { name: /Hide demo banner/i }))
})
})

test("error", async () => {
const user = userEvent.setup()

const mocks = [
{
request: {
query: UPDATE_WORKSPACE,
variables: {
id: "1",
},
},
result: {
errors: [new GraphQLError("Error!")],
},
},
]

render(<HideDemoWorkspace workspace={workspace} />, { mocks })

await act(async () => {
await user.click(screen.getByRole("button", { name: /Hide demo banner/i }))
})

await waitFor(() => {
expect(
screen.getByText("Failed to update workspace ApolloError: Error!"),
).toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from "react"
import { gql, useMutation } from "@apollo/client"
import { useSnackbar } from "notistack"
import {
UpdateWorkspaceSampleData,
UpdateWorkspaceSampleDataVariables,
} from "./__generated__/UpdateWorkspaceSampleData"
import DangerItem from "./DangerItem"

export const UPDATE_WORKSPACE = gql`
mutation UpdateWorkspaceSampleData($id: ID!) {
updateWorkspace(id: $id, sample_data: false) {
id
sample_data
}
}
`

interface Workspace {
id: string
}

type HideDemoWorkspaceProps = {
workspace: Workspace
}

const HideDemoWorkspace: React.FC<HideDemoWorkspaceProps> = ({ workspace }) => {
const { enqueueSnackbar } = useSnackbar()

const [updateWorkspace, { loading }] = useMutation<
UpdateWorkspaceSampleData,
UpdateWorkspaceSampleDataVariables
>(UPDATE_WORKSPACE, {
variables: { id: workspace.id },
})

const handleClick = () => {
updateWorkspace()
.then(() =>
enqueueSnackbar("Demo banners hidden", { variant: "success" }),
)
.catch(error =>
enqueueSnackbar(`Failed to update workspace ${error}`, {
variant: "error",
}),
)
}

return (
<DangerItem
onClick={handleClick}
loading={loading}
primary="Hide Demo Banners"
secondary="Hide all demo banners from this workspace"
buttonText="Hide Demo Banners"
/>
)
}

export default HideDemoWorkspace
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import WorkspaceDanger from "./WorkspaceDanger"
const workspace = {
id: "1",
name: "Test Workspace",
sample_data: false,
}

test("renders", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import React from "react"
import { Box, Grid, Typography } from "@mui/material"
import ClearWorkspace from "./ClearWorkspace"
import ClearWorkspaceCache from "./ClearWorkspaceCache"
import HideDemoWorkspace from "./HideDemoWorkspace"

export interface Workspace {
id: string
name: string
sample_data: boolean
}

type WorkspaceDangerProps = {
Expand All @@ -20,6 +22,7 @@ const WorkspaceDanger: React.FC<WorkspaceDangerProps> = ({ workspace }) => (
<Grid container>
<Grid item md={6}>
<ClearWorkspaceCache />
{workspace.sample_data && <HideDemoWorkspace workspace={workspace} />}
<ClearWorkspace workspace={workspace} />
</Grid>
</Grid>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ test("submit error", async () => {
workspace: {
id: "1",
name: "Test Workspace",
sample_data: false,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions grai-frontend/src/pages/settings/WorkspaceSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const GET_WORKSPACE = gql`
workspace(organisationName: $organisationName, name: $workspaceName) {
id
name
sample_data
}
}
`
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions grai-frontend/src/testing/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9849,15 +9849,21 @@
"name": "name",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
"defaultValue": "null"
},
{
"name": "sample_data",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"defaultValue": "null"
}
],
"type": {
Expand Down
13 changes: 9 additions & 4 deletions grai-server/app/workspaces/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ async def updateWorkspace(
self,
info: Info,
id: strawberry.ID,
name: str,
name: Optional[str] = None,
sample_data: Optional[bool] = None,
) -> Workspace:
validate_no_slash(name, "Workspace name")

workspace = await aget_workspace(info, id)

workspace.name = name
if name is not None:
validate_no_slash(name, "Workspace name")
workspace.name = name

if sample_data is not None:
workspace.sample_data = sample_data

await sync_to_async(workspace.save)()

return workspace
Expand Down
29 changes: 29 additions & 0 deletions grai-server/app/workspaces/tests/test_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ async def test_update_workspace(test_context):
}


@pytest.mark.django_db
async def test_update_workspace_sample_data(test_context):
context, organisation, workspace, user, membership = test_context

mutation = """
mutation UpdateWorkspace($id: ID!, $sample_data: Boolean!) {
updateWorkspace(id: $id, sample_data: $sample_data) {
id
sample_data
}
}
"""

result = await schema.execute(
mutation,
variable_values={
"id": str(workspace.id),
"sample_data": True,
},
context_value=context,
)

assert result.errors is None
assert result.data["updateWorkspace"] == {
"id": str(workspace.id),
"sample_data": True,
}


@pytest.mark.asyncio
@pytest.mark.django_db
async def test_clear_workspace(test_context):
Expand Down

0 comments on commit ab3bf85

Please sign in to comment.