|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { areFakerArgsValid, isValidFakerMethod } from './utils'; |
3 | 3 |
|
4 | | -describe('Mock Data Generator Utils', function () { |
| 4 | +import Sinon from 'sinon'; |
| 5 | +import { faker } from '@faker-js/faker/locale/en'; |
| 6 | +import { createNoopLogger } from '@mongodb-js/compass-logging/provider'; |
| 7 | + |
| 8 | +describe('Mock Data Generator Utils', () => { |
| 9 | + const sandbox = Sinon.createSandbox(); |
| 10 | + const logger = createNoopLogger(); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + sandbox.restore(); |
| 14 | + }); |
| 15 | + |
5 | 16 | describe('areFakerArgsValid', () => { |
6 | 17 | it('returns true for empty array', () => { |
7 | 18 | expect(areFakerArgsValid([])).to.be.true; |
@@ -79,93 +90,141 @@ describe('Mock Data Generator Utils', function () { |
79 | 90 | expect(areFakerArgsValid([obj])).to.be.false; |
80 | 91 | }); |
81 | 92 |
|
82 | | - describe('isValidFakerMethod', () => { |
83 | | - it('returns false for invalid method format', () => { |
84 | | - expect(isValidFakerMethod('invalidMethod', [])).to.deep.equal({ |
85 | | - isValid: false, |
86 | | - fakerArgs: [], |
87 | | - }); |
88 | | - expect(isValidFakerMethod('internet.email.extra', [])).to.deep.equal({ |
89 | | - isValid: false, |
90 | | - fakerArgs: [], |
91 | | - }); |
92 | | - }); |
| 93 | + it('returns false for deeply nested invalid structures with max depth', () => { |
| 94 | + const obj = { |
| 95 | + json: JSON.stringify({ |
| 96 | + a: [ |
| 97 | + 1, |
| 98 | + { |
| 99 | + json: JSON.stringify({ |
| 100 | + b: 2, |
| 101 | + c: { |
| 102 | + json: JSON.stringify({ |
| 103 | + d: 3, |
| 104 | + }), |
| 105 | + }, |
| 106 | + }), |
| 107 | + }, |
| 108 | + ], |
| 109 | + }), |
| 110 | + }; |
| 111 | + expect(areFakerArgsValid([obj])).to.be.false; |
| 112 | + }); |
| 113 | + }); |
93 | 114 |
|
94 | | - it('returns false for non-existent faker module', () => { |
95 | | - expect(isValidFakerMethod('notamodule.email', [])).to.deep.equal({ |
96 | | - isValid: false, |
97 | | - fakerArgs: [], |
98 | | - }); |
99 | | - }); |
| 115 | + describe('isValidFakerMethod', () => { |
| 116 | + it('returns false for invalid method format', () => { |
| 117 | + sandbox.stub(faker.internet, 'email'); |
100 | 118 |
|
101 | | - it('returns false for non-existent faker method', () => { |
102 | | - expect(isValidFakerMethod('internet.notamethod', [])).to.deep.equal({ |
103 | | - isValid: false, |
104 | | - fakerArgs: [], |
105 | | - }); |
| 119 | + expect(isValidFakerMethod('invalidMethod', [], logger)).to.deep.equal({ |
| 120 | + isValid: false, |
| 121 | + fakerArgs: [], |
106 | 122 | }); |
107 | | - |
108 | | - it('returns true for valid method without arguments', () => { |
109 | | - const result = isValidFakerMethod('internet.email', []); |
110 | | - expect(result.isValid).to.be.true; |
111 | | - expect(result.fakerArgs).to.deep.equal([]); |
| 123 | + expect( |
| 124 | + isValidFakerMethod('internet.email.extra', [], logger) |
| 125 | + ).to.deep.equal({ |
| 126 | + isValid: false, |
| 127 | + fakerArgs: [], |
112 | 128 | }); |
| 129 | + }); |
113 | 130 |
|
114 | | - it('returns true for valid method with valid arguments', () => { |
115 | | - // name.firstName takes optional gender argument |
116 | | - const result = isValidFakerMethod('name.firstName', ['female']); |
117 | | - expect(result.isValid).to.be.true; |
118 | | - expect(result.fakerArgs).to.deep.equal(['female']); |
| 131 | + it('returns false for non-existent faker module', () => { |
| 132 | + expect(isValidFakerMethod('notamodule.email', [], logger)).to.deep.equal({ |
| 133 | + isValid: false, |
| 134 | + fakerArgs: [], |
119 | 135 | }); |
| 136 | + }); |
120 | 137 |
|
121 | | - it('returns true for valid method with no args if args are invalid but fallback works', () => { |
122 | | - // internet.email does not take arguments, so passing one should fallback to [] |
123 | | - const result = isValidFakerMethod('internet.email', []); |
124 | | - expect(result.isValid).to.be.true; |
125 | | - expect(result.fakerArgs).to.deep.equal([]); |
| 138 | + it('returns false for non-existent faker method', () => { |
| 139 | + expect( |
| 140 | + isValidFakerMethod('internet.notamethod', [], logger) |
| 141 | + ).to.deep.equal({ |
| 142 | + isValid: false, |
| 143 | + fakerArgs: [], |
126 | 144 | }); |
| 145 | + }); |
127 | 146 |
|
128 | | - it('returns false for valid method with invalid arguments and fallback fails', () => { |
129 | | - // date.month expects at most one argument, passing an object will fail both attempts |
130 | | - const result = isValidFakerMethod('date.month', [ |
131 | | - { foo: 'bar' } as any, |
132 | | - ]); |
133 | | - expect(result.isValid).to.be.false; |
134 | | - expect(result.fakerArgs).to.deep.equal([]); |
135 | | - }); |
| 147 | + it('returns true for valid method without arguments', () => { |
| 148 | + sandbox.stub(faker.internet, 'email').returns('[email protected]'); |
136 | 149 |
|
137 | | - it('returns false for helpers methods except arrayElement', () => { |
138 | | - expect(isValidFakerMethod('helpers.fake', [])).to.deep.equal({ |
139 | | - isValid: false, |
140 | | - fakerArgs: [], |
141 | | - }); |
142 | | - expect(isValidFakerMethod('helpers.slugify', [])).to.deep.equal({ |
143 | | - isValid: false, |
144 | | - fakerArgs: [], |
145 | | - }); |
146 | | - }); |
| 150 | + const result = isValidFakerMethod('internet.email', [], logger); |
| 151 | + expect(result.isValid).to.be.true; |
| 152 | + expect(result.fakerArgs).to.deep.equal([]); |
| 153 | + }); |
147 | 154 |
|
148 | | - it('returns true for helpers.arrayElement with valid arguments', () => { |
149 | | - const arr = ['a', 'b', 'c']; |
150 | | - const result = isValidFakerMethod('helpers.arrayElement', [arr]); |
151 | | - expect(result.isValid).to.be.true; |
152 | | - expect(result.fakerArgs).to.deep.equal([arr]); |
153 | | - }); |
| 155 | + it('returns true for valid method with valid arguments', () => { |
| 156 | + sandbox.stub(faker.person, 'firstName'); |
154 | 157 |
|
155 | | - it('returns false for helpers.arrayElement with invalid arguments', () => { |
156 | | - // Exceeding max args length |
157 | | - const arr = Array(11).fill('x'); |
158 | | - const result = isValidFakerMethod('helpers.arrayElement', [arr]); |
159 | | - expect(result.isValid).to.be.false; |
160 | | - expect(result.fakerArgs).to.deep.equal([]); |
161 | | - }); |
| 158 | + const result = isValidFakerMethod('person.firstName', ['female'], logger); |
| 159 | + expect(result.isValid).to.be.true; |
| 160 | + expect(result.fakerArgs).to.deep.equal(['female']); |
| 161 | + }); |
| 162 | + |
| 163 | + it('returns true for valid method with no args if args are invalid but fallback works', () => { |
| 164 | + sandbox.stub(faker.internet, 'email').returns('[email protected]'); |
| 165 | + |
| 166 | + const result = isValidFakerMethod('internet.email', [], logger); |
| 167 | + expect(result.isValid).to.be.true; |
| 168 | + expect(result.fakerArgs).to.deep.equal([]); |
| 169 | + }); |
162 | 170 |
|
163 | | - it('returns false for method with invalid fakerArgs', () => { |
164 | | - // Passing Infinity as argument |
165 | | - const result = isValidFakerMethod('name.firstName', [Infinity]); |
166 | | - expect(result.isValid).to.be.false; |
167 | | - expect(result.fakerArgs).to.deep.equal([]); |
| 171 | + it('returns true valid method with invalid arguments and strips args', () => { |
| 172 | + sandbox.stub(faker.date, 'month').returns('February'); |
| 173 | + |
| 174 | + const result = isValidFakerMethod( |
| 175 | + 'date.month', |
| 176 | + [{ foo: 'bar' } as any], |
| 177 | + logger |
| 178 | + ); |
| 179 | + expect(result.isValid).to.be.true; |
| 180 | + expect(result.fakerArgs).to.deep.equal([]); |
| 181 | + }); |
| 182 | + |
| 183 | + it('returns false for helpers methods except arrayElement', () => { |
| 184 | + expect(isValidFakerMethod('helpers.fake', [], logger)).to.deep.equal({ |
| 185 | + isValid: false, |
| 186 | + fakerArgs: [], |
168 | 187 | }); |
| 188 | + expect(isValidFakerMethod('helpers.slugify', [], logger)).to.deep.equal({ |
| 189 | + isValid: false, |
| 190 | + fakerArgs: [], |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it('returns true for helpers.arrayElement with valid arguments', () => { |
| 195 | + sandbox.stub(faker.helpers, 'arrayElement').returns('a'); |
| 196 | + |
| 197 | + const arr = ['a', 'b', 'c']; |
| 198 | + const result = isValidFakerMethod('helpers.arrayElement', [arr], logger); |
| 199 | + expect(result.isValid).to.be.true; |
| 200 | + expect(result.fakerArgs).to.deep.equal([arr]); |
| 201 | + }); |
| 202 | + |
| 203 | + it('returns false for helpers.arrayElement with invalid arguments', () => { |
| 204 | + // Exceeding max args length |
| 205 | + const arr = Array(11).fill('x'); |
| 206 | + const result = isValidFakerMethod('helpers.arrayElement', [arr], logger); |
| 207 | + expect(result.isValid).to.be.false; |
| 208 | + expect(result.fakerArgs).to.deep.equal([]); |
| 209 | + }); |
| 210 | + |
| 211 | + it('returns true for valid method with invalid fakerArgs and strips args', () => { |
| 212 | + sandbox.stub(faker.person, 'firstName').returns('a'); |
| 213 | + |
| 214 | + // Passing Infinity as argument |
| 215 | + const result = isValidFakerMethod('person.firstName', [Infinity], logger); |
| 216 | + expect(result.isValid).to.be.true; |
| 217 | + expect(result.fakerArgs).to.deep.equal([]); |
| 218 | + }); |
| 219 | + |
| 220 | + it('returns false when calling a faker method fails', () => { |
| 221 | + sandbox |
| 222 | + .stub(faker.person, 'firstName') |
| 223 | + .throws(new Error('Invalid faker method')); |
| 224 | + |
| 225 | + const result = isValidFakerMethod('person.firstName', [], logger); |
| 226 | + expect(result.isValid).to.be.false; |
| 227 | + expect(result.fakerArgs).to.deep.equal([]); |
169 | 228 | }); |
170 | 229 | }); |
171 | 230 | }); |
0 commit comments