Skip to content

Commit d92df04

Browse files
committed
WIP DiffScene.spec testing still, functionality bug fix
1 parent a6a47ca commit d92df04

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

packages/api-explorer/src/scenes/DiffScene/DiffScene.spec.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import React from 'react'
2727
import { screen, waitFor } from '@testing-library/react'
2828
import userEvent from '@testing-library/user-event'
2929

30-
import type { SpecItem } from '@looker/sdk-codegen'
30+
import type { SpecItem, SpecList } from '@looker/sdk-codegen'
3131
import { useHistory } from 'react-router-dom'
3232
import * as routerLocation from 'react-router-dom'
3333
import type { Location } from 'history'
@@ -40,16 +40,38 @@ import {
4040
import { getApixAdaptor } from '../../utils'
4141
import { DiffScene } from './DiffScene'
4242

43+
// jest.mock('react-router', () => {
44+
// const ReactRouter = jest.requireActual('react-router')
45+
// return {
46+
// ...ReactRouter,
47+
// useHistory: jest.fn().mockReturnValue({ push: jest.fn(), location }),
48+
// useLocation: jest.fn().mockReturnValue({ pathname: '/', search: '' }),
49+
// }
50+
// })
51+
4352
jest.mock('react-router', () => {
53+
const mockLocation = {
54+
pathname: '/',
55+
search: '',
56+
}
4457
const ReactRouter = jest.requireActual('react-router')
4558
return {
4659
...ReactRouter,
47-
useHistory: jest.fn().mockReturnValue({ push: jest.fn(), location }),
48-
useLocation: jest.fn().mockReturnValue({ pathname: '/', search: '' }),
60+
useHistory: jest.fn().mockReturnValue({
61+
push: jest.fn((location) => {
62+
mockLocation.pathname = location.pathname
63+
mockLocation.search = location.search
64+
}),
65+
location,
66+
}),
67+
useLocation: jest.fn(() => ({
68+
pathname: jest.fn().mockReturnValue(mockLocation.pathname),
69+
search: jest.fn().mockReturnValue(mockLocation.search),
70+
})),
4971
}
5072
})
5173

52-
const specs = getLoadedSpecs()
74+
const specs = getLoadedSpecs() as SpecList
5375
class MockApixAdaptor {
5476
async fetchSpec(spec: SpecItem) {
5577
return new Promise(() => specs[spec.key])
@@ -78,9 +100,10 @@ describe('DiffScene', () => {
78100
const toggleNavigation = () => false
79101
test('toggling comparison option pushes param to url', async () => {
80102
const { push } = useHistory()
103+
jest.spyOn(reactRedux, 'useDispatch').mockReturnValue(mockDispatch)
81104
const store = createTestStore({
82105
specs: { specs, currentSpecKey: '3.1' },
83-
settings: { initialized: true },
106+
settings: { initialized: true, diffOptions: [] },
84107
})
85108
renderWithRouterAndReduxProvider(
86109
<DiffScene specs={specs} toggleNavigation={toggleNavigation} />,
@@ -94,11 +117,15 @@ describe('DiffScene', () => {
94117
})
95118
)
96119
await waitFor(() => {
97-
expect(push).toHaveBeenCalledWith({
120+
expect(push).toHaveBeenLastCalledWith({
98121
pathname: '/',
99122
search: 'opts=missing',
100123
})
101124
})
125+
expect(mockDispatch).toHaveBeenLastCalledWith({
126+
payload: { diffOptions: ['missing'] },
127+
type: 'settings/setDiffOptionsAction',
128+
})
102129
// TODO: test URL change leads to store dispatch? - change mock history push implementation to change our location
103130
// TODO: test that toggling another will push both options to store/url
104131
})

packages/api-explorer/src/scenes/DiffScene/DocDiff/DiffItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const DiffMethodLink: FC<DiffMethodLinkProps> = ({
5959
method,
6060
specKey,
6161
}) => {
62-
const { navigate } = useNavigation()
62+
const { navigateWithGlobalParams } = useNavigation()
6363

6464
if (!method) return <Heading as="h4">{`Missing in ${specKey}`}</Heading>
6565

@@ -70,7 +70,7 @@ export const DiffMethodLink: FC<DiffMethodLinkProps> = ({
7070
<DiffLink
7171
role="link"
7272
onClick={() => {
73-
navigate(path)
73+
navigateWithGlobalParams(path)
7474
}}
7575
>{`${method.name} for ${specKey}`}</DiffLink>
7676
)

packages/api-explorer/src/scenes/DiffScene/diffUtils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ export const diffToSpec = (
126126
export const getDiffOptionsFromUrl = (opts: string | null) => {
127127
// expect input to be a comma-delimited list as a string
128128
if (!opts) return null
129-
const diffOptions = []
129+
const diffOptions: string[] = []
130130
for (const option of opts.split(',')) {
131-
if (allDiffToggles.includes(option.toLowerCase())) {
131+
const op = option.toLowerCase()
132+
if (allDiffToggles.includes(op) && !diffOptions.includes(op)) {
132133
diffOptions.push(option.toLowerCase())
133134
}
134135
}

0 commit comments

Comments
 (0)