|
| 1 | +import { TreeListParentNode, TreeState } from '@stoplight/tree-list'; |
1 | 2 | import { shallow } from 'enzyme'; |
2 | 3 | import 'jest-enzyme'; |
3 | 4 | import { JSONSchema4 } from 'json-schema'; |
4 | 5 | import * as React from 'react'; |
| 6 | +import { SchemaTree } from '../../tree'; |
5 | 7 | import { metadataStore } from '../../tree/metadata'; |
6 | 8 | import { walk } from '../../tree/walk'; |
7 | 9 | import { SchemaTreeListNode } from '../../types'; |
@@ -122,4 +124,101 @@ describe('Property component', () => { |
122 | 124 | expect(wrapper.findWhere(el => /^\{\d\}$/.test(el.text())).first()).toHaveText('{0}'); |
123 | 125 | }); |
124 | 126 | }); |
| 127 | + |
| 128 | + describe('properties names', () => { |
| 129 | + test('given an object, should display names its properties', () => { |
| 130 | + const schema: JSONSchema4 = { |
| 131 | + properties: { |
| 132 | + foo: { |
| 133 | + type: 'string', |
| 134 | + }, |
| 135 | + }, |
| 136 | + }; |
| 137 | + |
| 138 | + const tree = new SchemaTree(schema, new TreeState(), { |
| 139 | + expandedDepth: Infinity, |
| 140 | + mergeAllOf: false, |
| 141 | + resolveRef: void 0, |
| 142 | + }); |
| 143 | + |
| 144 | + tree.populate(); |
| 145 | + |
| 146 | + const wrapper = shallow(<Property node={Array.from(tree)[1]} />); |
| 147 | + expect(wrapper.find('div').first()).toHaveText('foo'); |
| 148 | + }); |
| 149 | + |
| 150 | + test('given an array of objects, should display names of those properties', () => { |
| 151 | + const schema: JSONSchema4 = { |
| 152 | + type: 'array', |
| 153 | + items: { |
| 154 | + properties: { |
| 155 | + foo: { |
| 156 | + type: 'string', |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }; |
| 161 | + |
| 162 | + const tree = new SchemaTree(schema, new TreeState(), { |
| 163 | + expandedDepth: Infinity, |
| 164 | + mergeAllOf: false, |
| 165 | + resolveRef: void 0, |
| 166 | + }); |
| 167 | + |
| 168 | + tree.populate(); |
| 169 | + |
| 170 | + const wrapper = shallow(<Property node={Array.from(tree)[1]} />); |
| 171 | + expect(wrapper.find('div').first()).toHaveText('foo'); |
| 172 | + }); |
| 173 | + |
| 174 | + test('given a ref pointing at primitive type, should not display property name', () => { |
| 175 | + const schema: JSONSchema4 = { |
| 176 | + properties: { |
| 177 | + foo: { |
| 178 | + $ref: '#/properties/bar', |
| 179 | + }, |
| 180 | + bar: { |
| 181 | + type: 'string', |
| 182 | + }, |
| 183 | + }, |
| 184 | + }; |
| 185 | + |
| 186 | + const tree = new SchemaTree(schema, new TreeState(), { |
| 187 | + expandedDepth: Infinity, |
| 188 | + mergeAllOf: false, |
| 189 | + resolveRef: void 0, |
| 190 | + }); |
| 191 | + |
| 192 | + tree.populate(); |
| 193 | + tree.unwrap(Array.from(tree)[1] as TreeListParentNode); |
| 194 | + |
| 195 | + const wrapper = shallow(<Property node={Array.from(tree)[2]} />); |
| 196 | + expect(wrapper.find('div').first()).not.toExist(); |
| 197 | + }); |
| 198 | + |
| 199 | + xtest('given a ref pointing at complex type, should not display property name', () => { |
| 200 | + const schema: JSONSchema4 = { |
| 201 | + properties: { |
| 202 | + foo: { |
| 203 | + $ref: '#/properties/bar', |
| 204 | + }, |
| 205 | + bar: { |
| 206 | + type: 'object', |
| 207 | + }, |
| 208 | + }, |
| 209 | + }; |
| 210 | + |
| 211 | + const tree = new SchemaTree(schema, new TreeState(), { |
| 212 | + expandedDepth: Infinity, |
| 213 | + mergeAllOf: false, |
| 214 | + resolveRef: void 0, |
| 215 | + }); |
| 216 | + |
| 217 | + tree.populate(); |
| 218 | + tree.unwrap(Array.from(tree)[1] as TreeListParentNode); |
| 219 | + |
| 220 | + const wrapper = shallow(<Property node={Array.from(tree)[2]} />); |
| 221 | + expect(wrapper.find('div').first()).not.toExist(); |
| 222 | + }); |
| 223 | + }); |
125 | 224 | }); |
0 commit comments