Skip to content

Commit 53f73bb

Browse files
authored
COMPASS-1323: Backport COMPASS-1312: Speed up docs tab for array party dataset (#1118) (#1125)
* ✅ Add enzyme test to cover expandAll children * Make enzyme tests work with the CRUD jQuery dropdown Feels a little hacky. * 🎨 Allow EditableDocument state.expandAll to be initally set from props * ⏩ COMPASS-1312: Speed up docs tab for array party dataset So Compass doesn't render hundreds of fields for 20 documents, just the one document that user cares about. Yields a far better performance trade-off of around 2 seconds until Documents tab loads and 2 seconds to click and expand all fields, instead of 20 seconds to load the Documents tab. Tested on the array_party data set, see COMPASS-1235. * ✅ Add enzyme test for edit expand 🐛 * 🎨 Switch to component.setState() * 🐛 Fix expanding individual arrows in editing mode * 🎨✅ Test for the expanded flag independent of edit mode Which editing mode does not matter here. * 🐛 Fix EditableElement expanded state regression for all modes i.e. both view and edit mode.
1 parent 0c7cea2 commit 53f73bb

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

src/internal-packages/crud/lib/component/editable-document.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class EditableDocument extends React.Component {
6464
*/
6565
constructor(props) {
6666
super(props);
67-
this.doc = this.loadDocument(props.doc);
67+
this.doc = EditableDocument.loadDocument(props.doc);
6868
debug('props.doc', this.doc);
6969

7070
this.state = {
@@ -110,7 +110,7 @@ class EditableDocument extends React.Component {
110110
*
111111
* @returns {HadronDocument} The hadron document.
112112
*/
113-
loadDocument(doc) {
113+
static loadDocument(doc) {
114114
return new HadronDocument(doc);
115115
}
116116

@@ -271,7 +271,7 @@ class EditableDocument extends React.Component {
271271
* @param {Object} doc - The updated document.
272272
*/
273273
handleUpdateSuccess(doc) {
274-
this.doc = this.loadDocument(doc);
274+
this.doc = EditableDocument.loadDocument(doc);
275275
this.subscribeToDocumentEvents();
276276
setTimeout(() => {
277277
this.setState({ editing: false });

src/internal-packages/crud/lib/component/editable-element.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ class EditableElement extends React.Component {
233233
*/
234234
renderChildren() {
235235
const components = [];
236+
if (!this.state.expanded && !this.props.expandAll) {
237+
// COMPASS-1312 Lazily render children when user clicks on expand
238+
return components;
239+
}
236240
let index = 0;
237241
for (const element of this.element.elements) {
238242
components.push((

test/enzyme/_setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const exposedProperties = ['window', 'navigator', 'document'];
99

1010
global.document = jsdom('');
1111
global.window = document.defaultView;
12+
global.window.jQuery = require('jquery');
1213
Object.keys(document.defaultView).forEach((property) => {
1314
if (typeof global[property] === 'undefined') {
1415
exposedProperties.push(property);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint no-unused-vars: 0, no-unused-expressions: 0 */
2+
const app = require('hadron-app');
3+
const chai = require('chai');
4+
const chaiEnzyme = require('chai-enzyme');
5+
const expect = chai.expect;
6+
const React = require('react');
7+
const { mount } = require('enzyme');
8+
const AppRegistry = require('hadron-app-registry');
9+
10+
const EditableDocument = require('../../src/internal-packages/crud/lib/component/editable-document');
11+
const EditableElement = require('../../src/internal-packages/crud/lib/component/editable-element');
12+
13+
// use chai-enzyme assertions, see https://github.com/producthunt/chai-enzyme
14+
chai.use(chaiEnzyme());
15+
16+
describe('<EditableDocument />', function() {
17+
let appRegistry = app.appRegistry;
18+
let appInstance = app.instance;
19+
beforeEach(() => {
20+
// Mock the AppRegistry with a new one so tests don't complain about
21+
// appRegistry.getComponent (i.e. appRegistry being undefined)
22+
app.appRegistry = new AppRegistry();
23+
app.instance = {build: {version: '3.2.0'}};
24+
});
25+
afterEach(() => {
26+
// Restore properties on the global app, so they don't affect other tests
27+
app.appRegistry = appRegistry;
28+
app.instance = appInstance;
29+
});
30+
31+
const doc = {a: {b: {c: {d: 1}}}};
32+
it('if expandAll is true, renders children', () => {
33+
const component = mount(<EditableDocument doc={doc} />);
34+
component.setState({
35+
expandAll: true
36+
});
37+
const children = component.find(EditableElement);
38+
expect(children).to.be.of.length(4);
39+
});
40+
context('COMPASS_1306, if expandAll is false', () => {
41+
it('if expandAll is false, does not render children', () => {
42+
const component = mount(
43+
<EditableDocument doc={doc} />
44+
);
45+
component.setState({
46+
expandAll: false
47+
});
48+
const children = component.find(EditableElement);
49+
expect(children).to.be.of.length(1);
50+
});
51+
it('if expandAll is false but expand true, renders another child', () => {
52+
// Work around https://github.com/airbnb/enzyme/issues/361
53+
// by mounting the EditableElement rather than the EditableDocument,
54+
// note this requires the HadronDocument constructor
55+
const document = EditableDocument.loadDocument(doc);
56+
for (const element of document.elements) {
57+
const component = mount(
58+
<EditableElement element={element} />
59+
);
60+
let children = component.find(EditableElement);
61+
expect(children).to.be.of.length(1);
62+
component.setState({
63+
expanded: true
64+
});
65+
children = component.find(EditableElement);
66+
expect(children).to.be.of.length(2);
67+
}
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)