-
Notifications
You must be signed in to change notification settings - Fork 236
feat(compass-collection): CLOUDP-348131: Schema Editor Dropdowns: Add mongo type to faker method mapping CLOUDP-348131 #7425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dbeb256
0aaa8f1
73f6b49
b478cd2
85bf859
2ba4849
7228c83
d3baf49
e1d8a5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { expect } from 'chai'; | ||
import React from 'react'; | ||
import { | ||
screen, | ||
render, | ||
cleanup, | ||
waitFor, | ||
userEvent, | ||
} from '@mongodb-js/testing-library-compass'; | ||
import sinon from 'sinon'; | ||
import FakerMappingSelector from './faker-mapping-selector'; | ||
import { UNRECOGNIZED_FAKER_METHOD } from '../../modules/collection-tab'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Drive-by suggestion]: This made me realize, due to parallel implementation there's a slight mismatch with |
||
import { MONGO_TYPE_TO_FAKER_METHODS } from './constants'; | ||
import { MongoDBFieldTypeValues } from '@mongodb-js/compass-generative-ai'; | ||
import type { FakerArg } from './script-generation-utils'; | ||
|
||
const mockActiveJsonType = MongoDBFieldTypeValues.String; | ||
const mockActiveFakerFunction = 'lorem.word'; | ||
const mockActiveFakerArgs: Array<FakerArg> = []; | ||
const onJsonTypeSelectStub = sinon.stub(); | ||
const onFakerFunctionSelectStub = sinon.stub(); | ||
|
||
describe('FakerMappingSelector', () => { | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it('should display all MongoDB types in the dropdown', async () => { | ||
// Check that all MongoDB types from the constant are present | ||
const mongoTypes = Object.keys(MongoDBFieldTypeValues); | ||
|
||
render( | ||
<FakerMappingSelector | ||
activeJsonType={mockActiveJsonType} | ||
activeFakerFunction="lorem.word" | ||
activeFakerArgs={mockActiveFakerArgs} | ||
onJsonTypeSelect={onJsonTypeSelectStub} | ||
onFakerFunctionSelect={onFakerFunctionSelectStub} | ||
/> | ||
); | ||
|
||
const jsonTypeSelect = screen.getByLabelText('JSON Type'); | ||
userEvent.click(jsonTypeSelect); | ||
|
||
for (const type of mongoTypes) { | ||
await waitFor(() => { | ||
expect(screen.getByRole('option', { name: type })).to.exist; | ||
}); | ||
} | ||
}); | ||
|
||
describe('should display faker methods for each MongoDB type', () => { | ||
for (const [mongoType, methods] of Object.entries( | ||
MONGO_TYPE_TO_FAKER_METHODS | ||
)) { | ||
it(`should display faker methods for ${mongoType}`, () => { | ||
const firstMethod = methods[0]; | ||
|
||
render( | ||
<FakerMappingSelector | ||
activeJsonType={ | ||
mongoType as keyof typeof MONGO_TYPE_TO_FAKER_METHODS | ||
} | ||
activeFakerFunction={firstMethod} | ||
activeFakerArgs={mockActiveFakerArgs} | ||
onJsonTypeSelect={onJsonTypeSelectStub} | ||
onFakerFunctionSelect={onFakerFunctionSelectStub} | ||
/> | ||
); | ||
|
||
const fakerFunctionSelect = screen.getByLabelText('Faker Function'); | ||
userEvent.click(fakerFunctionSelect); | ||
|
||
methods.forEach((method) => { | ||
expect(screen.getByRole('option', { name: method })).to.exist; | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
it('should call onJsonTypeSelect when MongoDB type changes', async () => { | ||
render( | ||
<FakerMappingSelector | ||
activeJsonType={mockActiveJsonType} | ||
activeFakerFunction={mockActiveFakerFunction} | ||
activeFakerArgs={mockActiveFakerArgs} | ||
onJsonTypeSelect={onJsonTypeSelectStub} | ||
onFakerFunctionSelect={onFakerFunctionSelectStub} | ||
/> | ||
); | ||
|
||
const jsonTypeSelect = screen.getByLabelText('JSON Type'); | ||
userEvent.click(jsonTypeSelect); | ||
|
||
const numberOption = await screen.findByRole('option', { name: 'Number' }); | ||
userEvent.click(numberOption); | ||
|
||
expect(onJsonTypeSelectStub).to.have.been.calledOnceWith('Number'); | ||
}); | ||
|
||
it('should call onFakerFunctionSelect when faker function changes', async () => { | ||
render( | ||
<FakerMappingSelector | ||
activeJsonType={mockActiveJsonType} | ||
activeFakerFunction={mockActiveFakerFunction} | ||
activeFakerArgs={mockActiveFakerArgs} | ||
onJsonTypeSelect={onJsonTypeSelectStub} | ||
onFakerFunctionSelect={onFakerFunctionSelectStub} | ||
/> | ||
); | ||
|
||
const fakerFunctionSelect = screen.getByLabelText('Faker Function'); | ||
userEvent.click(fakerFunctionSelect); | ||
|
||
const emailOption = await screen.findByRole('option', { | ||
name: 'internet.email', | ||
}); | ||
userEvent.click(emailOption); | ||
|
||
expect(onFakerFunctionSelectStub).to.have.been.calledOnceWith( | ||
'internet.email' | ||
); | ||
}); | ||
|
||
it('should show warning banner when faker method is unrecognized', () => { | ||
render( | ||
<FakerMappingSelector | ||
activeJsonType={mockActiveJsonType} | ||
activeFakerFunction={UNRECOGNIZED_FAKER_METHOD} | ||
activeFakerArgs={mockActiveFakerArgs} | ||
onJsonTypeSelect={onJsonTypeSelectStub} | ||
onFakerFunctionSelect={onFakerFunctionSelectStub} | ||
/> | ||
); | ||
|
||
expect( | ||
screen.getByText( | ||
/Please select a function or we will default fill this field/ | ||
) | ||
).to.exist; | ||
}); | ||
|
||
it('should not show warning banner when faker method is recognized', () => { | ||
render( | ||
<FakerMappingSelector | ||
activeJsonType={mockActiveJsonType} | ||
activeFakerFunction={mockActiveFakerFunction} | ||
activeFakerArgs={mockActiveFakerArgs} | ||
onJsonTypeSelect={onJsonTypeSelectStub} | ||
onFakerFunctionSelect={onFakerFunctionSelectStub} | ||
/> | ||
); | ||
|
||
expect( | ||
screen.queryByText( | ||
/Please select a function or we will default fill this field/ | ||
) | ||
).to.not.exist; | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gave Augment link to https://v9.fakerjs.dev/api/ and had it come up with some more additions. (If you upgrade to v10 can do the same with the new API link):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding these!