-
Notifications
You must be signed in to change notification settings - Fork 12
Filter objects UI #489
Filter objects UI #489
Changes from 7 commits
47f5187
7352326
0328c4e
9c86658
87abd55
a41166d
615c8a8
f94f73c
d51c9cd
5398297
81b5801
daa56c7
3ac4de5
6cbdf96
7765403
5589096
43f4cdd
a02ea37
4fe13f9
e787283
0dc0f2d
7f41d96
3425188
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
import { fetchRecipeFilters } from 'console/state/recipes/actions'; | ||
|
||
@connect( | ||
null, | ||
{ | ||
fetchRecipeFilters, | ||
}, | ||
) | ||
class QueryRecipeFilters extends React.PureComponent { | ||
static propTypes = { | ||
fetchRecipeFilters: PropTypes.func.isRequired, | ||
}; | ||
|
||
componentDidMount() { | ||
this.props.fetchRecipeFilters(); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default QueryRecipeFilters; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { configure, mount, shallow } from 'enzyme'; | ||
import { cleanup } from 'react-testing-library'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import * as immutableMatchers from 'jest-immutable-matchers'; | ||
import fetchMock from 'fetch-mock'; | ||
import { Headers } from 'whatwg-fetch'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would probably be good to leave the space between imports and the rest of the code that used to be here. |
||
|
||
// Configure Enzyme adapter | ||
configure({ adapter: new Adapter() }); | ||
|
@@ -40,10 +42,12 @@ global.auth0 = { | |
})), | ||
}; | ||
global.localStorage = mockLocalStorage(); | ||
global.sessionStorage = mockLocalStorage(); | ||
global.mount = mount; | ||
global.shallow = shallow; | ||
global.React = React; | ||
global.ReactDOM = ReactDOM; | ||
global.Headers = Headers; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you don't do this you get unhandled errors in
You can hopefully see an example of that here: https://circleci.com/gh/mozilla/delivery-console/3515 It saddens me that the tests don't fail on this blatant error (that global There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, when using globals, should one use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or rather, to be more explicit. |
||
|
||
// Set up a global fetch mock, and assert that all calls to it are expected. | ||
beforeEach(() => { | ||
|
@@ -60,4 +64,7 @@ afterEach(() => { | |
} finally { | ||
fetchMock.restore(); | ||
} | ||
|
||
// Unmounts React trees that were mounted with react-testing-library's render. | ||
cleanup(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,13 +145,30 @@ export function fetchRecipeHistory(pk) { | |
|
||
export function fetchRecipeFilters() { | ||
return async dispatch => { | ||
const localStorageKey = 'recipe_filters'; | ||
const localFilters = window.localStorage.getItem(localStorageKey); | ||
if (localFilters) { | ||
// If the filters *were* in localStorage, it's a JSON *string*. | ||
dispatch({ | ||
type: RECIPE_FILTERS_RECEIVE, | ||
filters: JSON.parse(localFilters), | ||
}); | ||
} | ||
// XXX If we already had the filters in localStorage, can we avoid using this | ||
// makeNormandyApiRequest() with an ID so that it doesn't cause any spinners to appear | ||
// when the localStorage version almost definitely is good enough. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can we address this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check this out. In practice it works. I put in a Then I delete the stuff from |
||
const requestId = 'fetch-recipe-filters'; | ||
const filters = await dispatch(makeNormandyApiRequest(requestId, 'v2/filters/')); | ||
|
||
dispatch({ | ||
type: RECIPE_FILTERS_RECEIVE, | ||
filters, | ||
}); | ||
const filters = await dispatch(makeNormandyApiRequest(requestId, 'v3/filters/')); | ||
// After it has been retrieved remotely, it's very possible that the lists | ||
// haven't changed. If it hasn't changed compared to what we had in local Storage, then | ||
// don't bother dispatching again. | ||
if (!localFilters || JSON.stringify(filters) !== localFilters) { | ||
dispatch({ | ||
type: RECIPE_FILTERS_RECEIVE, | ||
filters, | ||
}); | ||
window.localStorage.setItem(localStorageKey, JSON.stringify(filters)); | ||
} | ||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { render } from 'react-testing-library'; | ||
import TestComponent from 'console/components/data/QueryRecipeFilters'; | ||
|
||
const { WrappedComponent: QueryRecipeFilters } = TestComponent; | ||
|
||
describe('<QueryRecipeFilters>', () => { | ||
it('should work', () => { | ||
const props = { | ||
fetchRecipeFilters: jest.fn(), | ||
}; | ||
const { container } = render(<QueryRecipeFilters {...props} />); | ||
expect(props.fetchRecipeFilters).toHaveBeenCalledTimes(1); | ||
// Should render nothing. | ||
expect(container.querySelectorAll('*').length).toEqual(0); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { fetchRecipeFilters } from 'console/state/recipes/actions'; | ||
|
||
describe('Actions', () => { | ||
describe('fetchRecipeFilters action', () => { | ||
it('should work (and update) on an empty localStorage', async () => { | ||
const fakeFilters = { countries: [] }; | ||
let state = {}; | ||
let dispatches = 0; | ||
const dispatcher = action => { | ||
if (typeof action === 'function') { | ||
// Let's pretend we call out to the network. | ||
return fakeFilters; | ||
} | ||
dispatches++; | ||
state = action; | ||
}; | ||
const func = fetchRecipeFilters(); | ||
await func(dispatcher); | ||
expect(state.filters).toEqual(fakeFilters); | ||
expect(dispatches).toEqual(1); | ||
expect(JSON.parse(global.localStorage.getItem('recipe_filters'))).toEqual(fakeFilters); | ||
}); | ||
|
||
it('should update existing localStorage', async () => { | ||
global.localStorage.setItem('recipe_filters', JSON.stringify({ countries: [] })); | ||
const fakeFilters = { countries: [{ key: 'SE', value: 'Sweeeden' }] }; | ||
let state = {}; | ||
let dispatches = 0; | ||
const dispatcher = action => { | ||
if (typeof action === 'function') { | ||
// Let's pretend we call out to the network. | ||
return fakeFilters; | ||
} | ||
dispatches++; | ||
state = action; | ||
}; | ||
const func = fetchRecipeFilters(); | ||
await func(dispatcher); | ||
expect(state.filters).toEqual(fakeFilters); | ||
expect(dispatches).toEqual(2); | ||
expect(JSON.parse(global.localStorage.getItem('recipe_filters'))).toEqual(fakeFilters); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't clear below what
cleanup
refers to. Can this beimport * as reactTestingLibrary from 'react-testing-library
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because JavaScript.
Look at the line above. It's called
configure
:)I don't like
import * as ... from '...'
because when tree shaking and partial imports can be used that's an anti-pattern.Also, I've seen a lot of code where they accept this mess and leave it as
import { commonword } from 'javascript-lacks-namespaces'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incidentally and whimsically, how about
is
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency is good, but repeating mistakes for the sake of consistency isn't useful. We can improve over time. Just because there are bad examples already in the code doesn't mean we can't do better going forward.