-
Notifications
You must be signed in to change notification settings - Fork 36
fix(typescript): propagate optional modifier for scalar fields #195
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -135,6 +135,39 @@ describe('TypescriptVisitor', function () { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mockSpecialVisit.calledWith(thing, param).should.be.ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should propagate optional modifier for scalar fields via parameters', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create a mock scalar field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mockScalarField = sinon.createStubInstance(Field); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mockScalarField.isOptional.returns(false); // Scalar field itself is not optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Create a thing that is a scalar type field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let thing = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isModelManager: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isModelFile: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isEnum: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isClassDeclaration: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isMapDeclaration: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isTypeScalar: () => true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isField: () => true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isRelationship: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isEnumValue: () => false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isOptional: () => true, // The parent field is optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getScalarField: () => mockScalarField | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mockSpecialVisit = sinon.stub(typescriptVisitor, 'visitField'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mockSpecialVisit.returns('Duck'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| typescriptVisitor.visit(thing, param); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify that visitField was called with the scalar field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mockSpecialVisit.calledOnce.should.be.ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Verify that forceOptional was passed via parameters (not by mutating the scalar field) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const callArgs = mockSpecialVisit.getCall(0).args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callArgs[0].should.equal(mockScalarField); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| callArgs[1].forceOptional.should.equal(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+158
to
+168
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mockSpecialVisit = sinon.stub(typescriptVisitor, 'visitField'); | |
| mockSpecialVisit.returns('Duck'); | |
| typescriptVisitor.visit(thing, param); | |
| // Verify that visitField was called with the scalar field | |
| mockSpecialVisit.calledOnce.should.be.ok; | |
| // Verify that forceOptional was passed via parameters (not by mutating the scalar field) | |
| const callArgs = mockSpecialVisit.getCall(0).args; | |
| callArgs[0].should.equal(mockScalarField); | |
| callArgs[1].forceOptional.should.equal(true); | |
| // Ensure the visitor has a FileWriter so that visitField can render output | |
| param.fileWriter = mockFileWriter; | |
| // Spy on visitField to exercise the real implementation while inspecting parameters | |
| let visitFieldSpy = sinon.spy(typescriptVisitor, 'visitField'); | |
| typescriptVisitor.visit(thing, param); | |
| // Verify that visitField was called with the scalar field | |
| visitFieldSpy.calledOnce.should.be.ok; | |
| // Verify that forceOptional was passed via parameters (not by mutating the scalar field) | |
| const callArgs = visitFieldSpy.getCall(0).args; | |
| callArgs[0].should.equal(mockScalarField); | |
| callArgs[1].forceOptional.should.equal(true); | |
| // Verify that the rendered output reflects the optional modifier (contains '?') | |
| mockFileWriter.writeLine.called.should.be.ok; | |
| const renderedLine = mockFileWriter.writeLine.getCall(0).args[0]; | |
| renderedLine.should.contain('?'); |
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.
This test does not assert the return value of
typescriptVisitor.visit(thing, param), unlike every other test in thevisitdescribe block, all of which assert.should.deep.equal('Duck'). The return value of the scalar path should also be verified.