Skip to content

Commit 8554618

Browse files
rueckstiessSatya
authored andcommitted
COMPASS-646 line-number.jsx: return component instead of plain object for bson types (#743)
* return component instead of plain object for array items. * add enzyme test. * clean up test. * fixed nested array/object inside array edge case.
1 parent a0d919b commit 8554618

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
"hadron-build": "^4.1.1",
185185
"jsdom": "^9.8.3",
186186
"mocha": "^3.1.2",
187+
"mock-require": "^2.0.1",
187188
"mongodb-js-precommit": "^0.2.9",
188189
"mongodb-runner": "^3.4.0",
189190
"react-addons-test-utils": "^15.2.1",

src/internal-packages/crud/lib/component/line-number.jsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const React = require('react');
22
const outsideClickable = require('react-click-outside');
3+
const getComponent = require('hadron-react-bson');
34
const Actions = require('../actions');
5+
// const debug = require('debug')('mongodb-compass:crud:line-number');
46

57
/**
68
* The BEM base style name for the element.
@@ -25,17 +27,17 @@ const DEFAULT_TEXT = 'Add Field After ';
2527
/**
2628
* Object text.
2729
*/
28-
const OBJECT_TEXT = 'Add Field To Object ';
30+
const OBJECT_TEXT = 'Add Field To ';
2931

3032
/**
3133
* Array text.
3234
*/
33-
const ARRAY_TEXT = 'Add Element To Array ';
35+
const ARRAY_TEXT = 'Add Array Element To ';
3436

3537
/**
3638
* Array element text.
3739
*/
38-
const ARRAY_ELEMENT_TEXT = 'Add Element After ';
40+
const ARRAY_ELEMENT_TEXT = 'Add Array Element After ';
3941

4042
/**
4143
* Add child icon.
@@ -197,12 +199,31 @@ class LineNumber extends React.Component {
197199
}
198200

199201
/**
200-
* Render the field name in the menu.
202+
* Render the value of the element.
203+
*
204+
* @returns {React.Component} The value component.
205+
*/
206+
renderValue() {
207+
const component = getComponent(this.props.element.currentType);
208+
return React.createElement(
209+
component,
210+
{ type: this.props.element.currentType, value: this.props.element.currentValue }
211+
);
212+
}
213+
214+
/**
215+
* Render the identifier in the menu. For objects and arrays in an array,
216+
* this is the type, because the type is already part of the message. For other
217+
* values inside arrays, it's the actual value. Otherwise it's the key.
201218
*
202219
* @returns {String} The field name or value if an array element.
203220
*/
204-
renderFieldName() {
205-
return this.props.element.currentKey || this.props.element.currentValue;
221+
renderIdentifier() {
222+
// this case is already handled in renderDefaultItem()
223+
if (this.isParentArray() && (this.isElementObject() || this.isElementArray())) {
224+
return this.props.element.currentType;
225+
}
226+
return this.props.element.currentKey || this.renderValue();
206227
}
207228

208229
/**
@@ -212,14 +233,16 @@ class LineNumber extends React.Component {
212233
* @param {String} text - The text.
213234
* @param {Function} handler - The click handler.
214235
* @param {String} testId - The test id.
236+
*
237+
* @returns {Component} the menu item component
215238
*/
216239
renderMenuItem(iconClassName, text, handler, testId) {
217240
return (
218241
<li onClick={handler} data-test-id={testId}>
219242
<span>
220243
<i className={iconClassName} />
221244
{text}
222-
<span className={FIELD_NAME_CLASS}>{this.renderFieldName()}</span>
245+
<span className={FIELD_NAME_CLASS}>{this.renderIdentifier()}</span>
223246
</span>
224247
</li>
225248
);

test/crud.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint no-unused-vars: 0 */
2+
3+
const mock = require('mock-require');
4+
const sinon = require('sinon');
5+
const chai = require('chai');
6+
const chaiEnzyme = require('chai-enzyme');
7+
const expect = chai.expect;
8+
const bson = require('bson');
9+
const mount = require('enzyme').mount;
10+
11+
// mock CRUD actions
12+
const closeAllMenusSpy = sinon.spy();
13+
closeAllMenusSpy.listen = () => {};
14+
15+
const crudActions = {
16+
documentRemoved: sinon.spy(),
17+
openInsertDocumentDialog: sinon.spy(),
18+
closeInsertDocumentDialog: sinon.spy(),
19+
insertDocument: sinon.spy(),
20+
fileDropped: sinon.spy(),
21+
refreshDocuments: sinon.spy(),
22+
closeAllMenus: closeAllMenusSpy
23+
};
24+
25+
mock('../src/internal-packages/crud/lib/actions', crudActions);
26+
27+
const React = require('react');
28+
const Element = require('hadron-document').Element;
29+
const LineNumber = require('../src/internal-packages/crud/lib/component/line-number');
30+
31+
// use chai-enzyme assertions, see https://github.com/producthunt/chai-enzyme
32+
chai.use(chaiEnzyme());
33+
34+
describe('CRUD', function() {
35+
const element = new Element(undefined, bson.Long.fromString('1'), false, {isRoot: () => false});
36+
37+
describe('<LineNumber />', function() {
38+
it('renders a hadron-react-bson component for array values (COMPASS-646)', function() {
39+
const component = mount(<LineNumber element={element} />);
40+
expect(component.find('span.line-number-menu-field div.element-value')).to.have.className('element-value-is-int64');
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)