Skip to content

Commit

Permalink
Uplift and export SeekApiResponse (#812)
Browse files Browse the repository at this point in the history
This now automatically removes `__typename`s.
  • Loading branch information
72636c authored Aug 16, 2022
1 parent 239c938 commit 5f12929
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-readers-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wingman-fe': minor
---

prettyPrintWithoutTypename: Export new function
5 changes: 5 additions & 0 deletions .changeset/three-timers-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wingman-fe': minor
---

SeekApiResponse: Export new component
7 changes: 4 additions & 3 deletions fe/lib/components/JobCategoryLookup/JobCategoryLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ export const JobCategoryLookup = ({
).map((x) => ({ name: x.name, key: x.id.value }))}
/>

<SeekApiResponse id="jobCategoryLookupSeekApiResponse">
{categoryData.jobCategory}
</SeekApiResponse>
<SeekApiResponse
data={categoryData.jobCategory}
id="jobCategoryLookupSeekApiResponse"
/>
</>
) : (
<Notice tone="info">
Expand Down
7 changes: 4 additions & 3 deletions fe/lib/components/LocationLookup/LocationLookup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ export const LocationLookup = ({
).map((x) => ({ name: x.name, key: x.id.value }))}
/>

<SeekApiResponse id="locationLookupSeekApiResponse">
{locationData.location}
</SeekApiResponse>
<SeekApiResponse
data={locationData.location}
id="locationLookupSeekApiResponse"
/>
</>
) : (
<Notice tone="info">
Expand Down
72 changes: 72 additions & 0 deletions fe/lib/components/SeekApiResponse/SeekApiResponse.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'braid-design-system/reset';

import { Heading, Stack } from 'braid-design-system';
import React, { ComponentProps } from 'react';
import { CodeBlock } from 'scoobie';

import {
BraidArgs,
defaultArgTypes,
defaultArgs,
} from '../../storybook/controls';
import { BraidStorybookProvider } from '../../storybook/decorators';

import { SeekApiResponse as Component } from './SeekApiResponse';

export default {
args: {
braidThemeName: defaultArgs.braidThemeName,
},
argTypes: {
braidThemeName: defaultArgTypes.braidThemeName,
data: {
control: false,
},
},
component: Component,
title: 'Utils/SeekApiResponse',
};

type ComponentPropsWithoutData = Omit<ComponentProps<typeof Component>, 'data'>;

type Args = ComponentPropsWithoutData & BraidArgs;

export const SeekApiResponse = ({ braidThemeName, ...args }: Args) => (
<BraidStorybookProvider braidThemeName={braidThemeName}>
<Stack dividers space="large">
{[
{
data: {},
heading: 'Empty object',
},
{
data: {
id: {
value: 'abc',
},
},
heading: 'Object without __typenames',
},
{
data: {
__typename: 'Event',
id: {
__typename: 'ObjectIdentifier',
value: 'abc',
},
},
heading: 'Object with nested __typenames',
},
].map(({ data, heading }) => (
<Stack key={heading} space="large">
<Heading level="3">{heading}</Heading>

<CodeBlock language="json">{JSON.stringify(data, null, 2)}</CodeBlock>

<Component {...args} data={data} />
</Stack>
))}
</Stack>
</BraidStorybookProvider>
);
SeekApiResponse.storyName = 'SeekApiResponse';
34 changes: 29 additions & 5 deletions fe/lib/components/SeekApiResponse/SeekApiResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,41 @@ import { Disclosure } from 'braid-design-system';
import React from 'react';
import { CodeBlock } from 'scoobie';

import { prettyPrintWithoutTypename } from './prettyPrintWithoutTypename';

interface Props {
children: unknown;
/**
* The label of the underlying disclosure component when it can be collapsed.
*/
collapseLabel?: string;

/**
* The JSON response data from the SEEK API.
*/
data: unknown;

/**
* The label of the underlying disclosure component when it can be expanded.
*/
expandLabel?: string;

/**
* The DOM identifier of the underlying disclosure component.
*/
id: string;
}

export const SeekApiResponse = ({ children, id }: Props) => (
export const SeekApiResponse = ({
collapseLabel,
data,
expandLabel,
id,
}: Props) => (
<Disclosure
collapseLabel="Hide SEEK API response"
expandLabel="Show SEEK API response"
collapseLabel={collapseLabel ?? 'Hide SEEK API response'}
expandLabel={expandLabel ?? 'Show SEEK API response'}
id={id}
>
<CodeBlock language="json">{JSON.stringify(children, null, 2)}</CodeBlock>
<CodeBlock language="json">{prettyPrintWithoutTypename(data)}</CodeBlock>
</Disclosure>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prettyPrintWithoutTypename empty object 1`] = `"{}"`;

exports[`prettyPrintWithoutTypename object with nested __typenames 1`] = `
"{
\\"id\\": {
\\"value\\": 123
}
}"
`;

exports[`prettyPrintWithoutTypename object without __typenames 1`] = `
"{
\\"id\\": {
\\"value\\": 123
}
}"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { prettyPrintWithoutTypename } from './prettyPrintWithoutTypename';

describe('prettyPrintWithoutTypename', () => {
it.each`
description | input
${'empty object'} | ${{}}
${'object without __typenames'} | ${{ id: { value: 123 } }}
${'object with nested __typenames'} | ${{ __typename: 'Event', id: { __typename: 'ObjectIdentifier', value: 123 } }}
`('$description', ({ input }) =>
expect(prettyPrintWithoutTypename(input)).toMatchSnapshot(),
);
});
10 changes: 10 additions & 0 deletions fe/lib/components/SeekApiResponse/prettyPrintWithoutTypename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Pretty prints a JSON value with recursively stripping of the GraphQL
* `__typename` field.
*/
export const prettyPrintWithoutTypename = (data: unknown) =>
JSON.stringify(
data,
(name, value) => (name === '__typename' ? undefined : value),
2,
);
3 changes: 3 additions & 0 deletions fe/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ export { MockSalaryDetails } from './components/SalaryDetails/SalaryDetails.mock
export { SpecifiedPersonForm } from './components/SpecifiedPersonForm/SpecifiedPersonForm';
export { MockSpecifiedPersonForm } from './components/SpecifiedPersonForm/SpecifiedPersonForm.mock';

export { SeekApiResponse } from './components/SeekApiResponse/SeekApiResponse';
export { prettyPrintWithoutTypename } from './components/SeekApiResponse/prettyPrintWithoutTypename';

export { apolloTypePolicies } from './types/apolloTypePolicies';

0 comments on commit 5f12929

Please sign in to comment.