|  | 
|  | 1 | +import { expect } from 'chai'; | 
|  | 2 | +import React from 'react'; | 
|  | 3 | +import { | 
|  | 4 | +  screen, | 
|  | 5 | +  render, | 
|  | 6 | +  cleanup, | 
|  | 7 | +  waitFor, | 
|  | 8 | +  userEvent, | 
|  | 9 | +} from '@mongodb-js/testing-library-compass'; | 
|  | 10 | +import sinon from 'sinon'; | 
|  | 11 | +import FakerMappingSelector from './faker-mapping-selector'; | 
|  | 12 | +import { UNRECOGNIZED_FAKER_METHOD } from '../../modules/collection-tab'; | 
|  | 13 | +import { MONGO_TYPE_TO_FAKER_METHODS } from './constants'; | 
|  | 14 | +import { MongoDBFieldTypeValues } from '@mongodb-js/compass-generative-ai'; | 
|  | 15 | +import type { FakerArg } from './script-generation-utils'; | 
|  | 16 | + | 
|  | 17 | +const mockActiveJsonType = MongoDBFieldTypeValues.String; | 
|  | 18 | +const mockActiveFakerFunction = 'lorem.word'; | 
|  | 19 | +const mockActiveFakerArgs: Array<FakerArg> = []; | 
|  | 20 | +const onJsonTypeSelectStub = sinon.stub(); | 
|  | 21 | +const onFakerFunctionSelectStub = sinon.stub(); | 
|  | 22 | + | 
|  | 23 | +describe('FakerMappingSelector', () => { | 
|  | 24 | +  afterEach(() => { | 
|  | 25 | +    cleanup(); | 
|  | 26 | +  }); | 
|  | 27 | + | 
|  | 28 | +  it('should display all MongoDB types in the dropdown', async () => { | 
|  | 29 | +    // Check that all MongoDB types from the constant are present | 
|  | 30 | +    const mongoTypes = Object.keys(MongoDBFieldTypeValues); | 
|  | 31 | + | 
|  | 32 | +    render( | 
|  | 33 | +      <FakerMappingSelector | 
|  | 34 | +        activeJsonType={mockActiveJsonType} | 
|  | 35 | +        activeFakerFunction="lorem.word" | 
|  | 36 | +        activeFakerArgs={mockActiveFakerArgs} | 
|  | 37 | +        onJsonTypeSelect={onJsonTypeSelectStub} | 
|  | 38 | +        onFakerFunctionSelect={onFakerFunctionSelectStub} | 
|  | 39 | +      /> | 
|  | 40 | +    ); | 
|  | 41 | + | 
|  | 42 | +    const jsonTypeSelect = screen.getByLabelText('JSON Type'); | 
|  | 43 | +    userEvent.click(jsonTypeSelect); | 
|  | 44 | + | 
|  | 45 | +    for (const type of mongoTypes) { | 
|  | 46 | +      await waitFor(() => { | 
|  | 47 | +        expect(screen.getByRole('option', { name: type })).to.exist; | 
|  | 48 | +      }); | 
|  | 49 | +    } | 
|  | 50 | +  }); | 
|  | 51 | + | 
|  | 52 | +  describe('should display faker methods for each MongoDB type', () => { | 
|  | 53 | +    for (const [mongoType, methods] of Object.entries( | 
|  | 54 | +      MONGO_TYPE_TO_FAKER_METHODS | 
|  | 55 | +    )) { | 
|  | 56 | +      it(`should display faker methods for ${mongoType}`, () => { | 
|  | 57 | +        const firstMethod = methods[0]; | 
|  | 58 | + | 
|  | 59 | +        render( | 
|  | 60 | +          <FakerMappingSelector | 
|  | 61 | +            activeJsonType={ | 
|  | 62 | +              mongoType as keyof typeof MONGO_TYPE_TO_FAKER_METHODS | 
|  | 63 | +            } | 
|  | 64 | +            activeFakerFunction={firstMethod} | 
|  | 65 | +            activeFakerArgs={mockActiveFakerArgs} | 
|  | 66 | +            onJsonTypeSelect={onJsonTypeSelectStub} | 
|  | 67 | +            onFakerFunctionSelect={onFakerFunctionSelectStub} | 
|  | 68 | +          /> | 
|  | 69 | +        ); | 
|  | 70 | + | 
|  | 71 | +        const fakerFunctionSelect = screen.getByLabelText('Faker Function'); | 
|  | 72 | +        userEvent.click(fakerFunctionSelect); | 
|  | 73 | + | 
|  | 74 | +        methods.forEach((method) => { | 
|  | 75 | +          expect(screen.getByRole('option', { name: method })).to.exist; | 
|  | 76 | +        }); | 
|  | 77 | +      }); | 
|  | 78 | +    } | 
|  | 79 | +  }); | 
|  | 80 | + | 
|  | 81 | +  it('should call onJsonTypeSelect when MongoDB type changes', async () => { | 
|  | 82 | +    render( | 
|  | 83 | +      <FakerMappingSelector | 
|  | 84 | +        activeJsonType={mockActiveJsonType} | 
|  | 85 | +        activeFakerFunction={mockActiveFakerFunction} | 
|  | 86 | +        activeFakerArgs={mockActiveFakerArgs} | 
|  | 87 | +        onJsonTypeSelect={onJsonTypeSelectStub} | 
|  | 88 | +        onFakerFunctionSelect={onFakerFunctionSelectStub} | 
|  | 89 | +      /> | 
|  | 90 | +    ); | 
|  | 91 | + | 
|  | 92 | +    const jsonTypeSelect = screen.getByLabelText('JSON Type'); | 
|  | 93 | +    userEvent.click(jsonTypeSelect); | 
|  | 94 | + | 
|  | 95 | +    const numberOption = await screen.findByRole('option', { name: 'Number' }); | 
|  | 96 | +    userEvent.click(numberOption); | 
|  | 97 | + | 
|  | 98 | +    expect(onJsonTypeSelectStub).to.have.been.calledOnceWith('Number'); | 
|  | 99 | +  }); | 
|  | 100 | + | 
|  | 101 | +  it('should call onFakerFunctionSelect when faker function changes', async () => { | 
|  | 102 | +    render( | 
|  | 103 | +      <FakerMappingSelector | 
|  | 104 | +        activeJsonType={mockActiveJsonType} | 
|  | 105 | +        activeFakerFunction={mockActiveFakerFunction} | 
|  | 106 | +        activeFakerArgs={mockActiveFakerArgs} | 
|  | 107 | +        onJsonTypeSelect={onJsonTypeSelectStub} | 
|  | 108 | +        onFakerFunctionSelect={onFakerFunctionSelectStub} | 
|  | 109 | +      /> | 
|  | 110 | +    ); | 
|  | 111 | + | 
|  | 112 | +    const fakerFunctionSelect = screen.getByLabelText('Faker Function'); | 
|  | 113 | +    userEvent.click(fakerFunctionSelect); | 
|  | 114 | + | 
|  | 115 | +    const emailOption = await screen.findByRole('option', { | 
|  | 116 | +      name: 'internet.email', | 
|  | 117 | +    }); | 
|  | 118 | +    userEvent.click(emailOption); | 
|  | 119 | + | 
|  | 120 | +    expect(onFakerFunctionSelectStub).to.have.been.calledOnceWith( | 
|  | 121 | +      'internet.email' | 
|  | 122 | +    ); | 
|  | 123 | +  }); | 
|  | 124 | + | 
|  | 125 | +  it('should show warning banner when faker method is unrecognized', () => { | 
|  | 126 | +    render( | 
|  | 127 | +      <FakerMappingSelector | 
|  | 128 | +        activeJsonType={mockActiveJsonType} | 
|  | 129 | +        activeFakerFunction={UNRECOGNIZED_FAKER_METHOD} | 
|  | 130 | +        activeFakerArgs={mockActiveFakerArgs} | 
|  | 131 | +        onJsonTypeSelect={onJsonTypeSelectStub} | 
|  | 132 | +        onFakerFunctionSelect={onFakerFunctionSelectStub} | 
|  | 133 | +      /> | 
|  | 134 | +    ); | 
|  | 135 | + | 
|  | 136 | +    expect( | 
|  | 137 | +      screen.getByText( | 
|  | 138 | +        /Please select a function or we will default fill this field/ | 
|  | 139 | +      ) | 
|  | 140 | +    ).to.exist; | 
|  | 141 | +  }); | 
|  | 142 | + | 
|  | 143 | +  it('should not show warning banner when faker method is recognized', () => { | 
|  | 144 | +    render( | 
|  | 145 | +      <FakerMappingSelector | 
|  | 146 | +        activeJsonType={mockActiveJsonType} | 
|  | 147 | +        activeFakerFunction={mockActiveFakerFunction} | 
|  | 148 | +        activeFakerArgs={mockActiveFakerArgs} | 
|  | 149 | +        onJsonTypeSelect={onJsonTypeSelectStub} | 
|  | 150 | +        onFakerFunctionSelect={onFakerFunctionSelectStub} | 
|  | 151 | +      /> | 
|  | 152 | +    ); | 
|  | 153 | + | 
|  | 154 | +    expect( | 
|  | 155 | +      screen.queryByText( | 
|  | 156 | +        /Please select a function or we will default fill this field/ | 
|  | 157 | +      ) | 
|  | 158 | +    ).to.not.exist; | 
|  | 159 | +  }); | 
|  | 160 | +}); | 
0 commit comments