1 | /* eslint-env mocha */
|
---|
2 | import assert from 'assert';
|
---|
3 | import { getOpeningElement, setParserName } from '../helper';
|
---|
4 | import getProp from '../../src/getProp';
|
---|
5 |
|
---|
6 | describe('getProp', () => {
|
---|
7 | beforeEach(() => {
|
---|
8 | setParserName('babel');
|
---|
9 | });
|
---|
10 | it('should export a function', () => {
|
---|
11 | const expected = 'function';
|
---|
12 | const actual = typeof getProp;
|
---|
13 |
|
---|
14 | assert.equal(actual, expected);
|
---|
15 | });
|
---|
16 |
|
---|
17 | it('should return undefined if no arguments are provided', () => {
|
---|
18 | const expected = undefined;
|
---|
19 | const actual = getProp();
|
---|
20 |
|
---|
21 | assert.equal(actual, expected);
|
---|
22 | });
|
---|
23 |
|
---|
24 | it('should return undefined if the attribute is absent', () => {
|
---|
25 | const code = '<div />';
|
---|
26 | const node = getOpeningElement(code);
|
---|
27 | const { attributes: props } = node;
|
---|
28 | const prop = 'id';
|
---|
29 |
|
---|
30 | const expected = undefined;
|
---|
31 | const actual = getProp(props, prop);
|
---|
32 |
|
---|
33 | assert.equal(actual, expected);
|
---|
34 | });
|
---|
35 |
|
---|
36 | it('should return the correct attribute if the attribute exists', () => {
|
---|
37 | const code = '<div id="foo" />';
|
---|
38 | const node = getOpeningElement(code);
|
---|
39 | const { attributes: props } = node;
|
---|
40 | const prop = 'id';
|
---|
41 |
|
---|
42 | const expected = 'id';
|
---|
43 | const actual = getProp(props, prop).name.name;
|
---|
44 |
|
---|
45 | assert.equal(actual, expected);
|
---|
46 | });
|
---|
47 |
|
---|
48 | it('should return the correct attribute if the attribute exists in spread', () => {
|
---|
49 | const code = '<div {...{ id: "foo" }} />';
|
---|
50 | const node = getOpeningElement(code);
|
---|
51 | const { attributes: props } = node;
|
---|
52 | const prop = 'ID';
|
---|
53 |
|
---|
54 | const expected = 'id';
|
---|
55 | const actual = getProp(props, prop).name.name;
|
---|
56 |
|
---|
57 | assert.equal(actual, expected);
|
---|
58 | });
|
---|
59 |
|
---|
60 | it('should return the correct attribute if the attribute exists in spread as an expression', () => {
|
---|
61 | const code = '<div {...{ id }} />';
|
---|
62 | const node = getOpeningElement(code);
|
---|
63 | const { attributes: props } = node;
|
---|
64 | const prop = 'id';
|
---|
65 |
|
---|
66 | const expected = 'id';
|
---|
67 | const actual = getProp(props, prop);
|
---|
68 | const actualName = actual.name.name;
|
---|
69 | const actualValue = actual.value.expression.name;
|
---|
70 |
|
---|
71 | assert.equal(actualName, expected);
|
---|
72 | assert.equal(actualValue, expected);
|
---|
73 | });
|
---|
74 |
|
---|
75 | it('should return the correct attribute if the attribute exists in spread (case sensitive)', () => {
|
---|
76 | const code = '<div {...{ id: "foo" }} />';
|
---|
77 | const node = getOpeningElement(code);
|
---|
78 | const { attributes: props } = node;
|
---|
79 | const prop = 'id';
|
---|
80 | const options = { ignoreCase: false };
|
---|
81 |
|
---|
82 | const expected = 'id';
|
---|
83 | const actual = getProp(props, prop, options).name.name;
|
---|
84 |
|
---|
85 | assert.equal(actual, expected);
|
---|
86 | });
|
---|
87 |
|
---|
88 | it('should return undefined if the attribute does not exist in spread (case sensitive)', () => {
|
---|
89 | const code = '<div {...{ id: "foo" }} />';
|
---|
90 | const node = getOpeningElement(code);
|
---|
91 | const { attributes: props } = node;
|
---|
92 | const prop = 'ID';
|
---|
93 | const options = { ignoreCase: false };
|
---|
94 |
|
---|
95 | const expected = undefined;
|
---|
96 | const actual = getProp(props, prop, options);
|
---|
97 |
|
---|
98 | assert.equal(actual, expected);
|
---|
99 | });
|
---|
100 |
|
---|
101 | it('should return undefined for key in spread', () => {
|
---|
102 | // https://github.com/reactjs/rfcs/pull/107
|
---|
103 | const code = '<div {...{ key }} />';
|
---|
104 | const node = getOpeningElement(code);
|
---|
105 | const { attributes: props } = node;
|
---|
106 | const prop = 'key';
|
---|
107 |
|
---|
108 | const expected = undefined;
|
---|
109 | const actual = getProp(props, prop);
|
---|
110 |
|
---|
111 | assert.equal(actual, expected);
|
---|
112 | });
|
---|
113 |
|
---|
114 | it('should return undefined if the attribute may exist in spread', () => {
|
---|
115 | const code = '<div {...props} />';
|
---|
116 | const node = getOpeningElement(code);
|
---|
117 | const { attributes: props } = node;
|
---|
118 | const prop = 'id';
|
---|
119 |
|
---|
120 | const expected = undefined;
|
---|
121 | const actual = getProp(props, prop);
|
---|
122 |
|
---|
123 | assert.equal(actual, expected);
|
---|
124 | });
|
---|
125 |
|
---|
126 | it('should not crash if the spread contains a spread', () => {
|
---|
127 | const code = '<div {...{ ...props }} />';
|
---|
128 | const node = getOpeningElement(code);
|
---|
129 | const { attributes: props } = node;
|
---|
130 | const prop = 'id';
|
---|
131 |
|
---|
132 | getProp(props, prop);
|
---|
133 | });
|
---|
134 |
|
---|
135 | it('should return undefined if the attribute is considered absent in case-sensitive mode', () => {
|
---|
136 | const code = '<div ID="foo" />';
|
---|
137 | const node = getOpeningElement(code);
|
---|
138 | const { attributes: props } = node;
|
---|
139 | const prop = 'id';
|
---|
140 | const options = {
|
---|
141 | ignoreCase: false,
|
---|
142 | };
|
---|
143 |
|
---|
144 | const expected = undefined;
|
---|
145 | const actual = getProp(props, prop, options);
|
---|
146 |
|
---|
147 | assert.equal(actual, expected);
|
---|
148 | });
|
---|
149 | });
|
---|