-
-
Notifications
You must be signed in to change notification settings - Fork 382
test: add unit tests for modelspresets.js #1950
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0d26fb
added tests for ModelsPresets.js
Ishita-190 09515d4
Merge branch 'asyncapi:master' into ishi
Ishita-190 16e4f69
Merge branch 'master' into ishi
Adi-204 a555469
fixed lint errors
Ishita-190 7516cb3
Merge branch 'asyncapi:master' into ishi
Ishita-190 bedce47
Merge branch 'ishi' of https://github.com/Ishita-190/generator_work i…
Ishita-190 7469b85
Added test case for model with missing properties key
Ishita-190 280b8c0
Merge branch 'master' into ishi
Adi-204 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| const { JavaModelsPresets } = require('../src/ModelsPresets'); | ||
| const { JAVA_COMMON_PRESET } = require('@asyncapi/modelina'); | ||
|
|
||
| const PACKAGE_DECLARATION = 'package com.asyncapi.models;'; | ||
| const IMPORT_OBJECTS = 'import java.util.Objects;'; | ||
| const IMPORT_MAP = 'import java.util.Map;'; | ||
| const TEST_CLASS_CONTENT = 'public class TestClass {}'; | ||
| const MAP_STRING_OBJECT_TYPE = 'Map<String, Object>'; | ||
| const PACKAGE_WITH_NEWLINES = 'package com.asyncapi.models;\n\n'; | ||
|
|
||
| describe('ModelsPresets', () => { | ||
| describe('JavaModelsPresets structure', () => { | ||
| it('should export JavaModelsPresets as an array', () => { | ||
| expect(Array.isArray(JavaModelsPresets)).toBe(true); | ||
| expect(JavaModelsPresets).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('should contain JAVA_COMMON_PRESET with correct options', () => { | ||
| const commonPreset = JavaModelsPresets[0]; | ||
| expect(commonPreset.preset).toBe(JAVA_COMMON_PRESET); | ||
| expect(commonPreset.options).toEqual({ | ||
| equal: true, | ||
| hashCode: true, | ||
| classToString: true, | ||
| marshalling: false | ||
| }); | ||
| }); | ||
|
|
||
| it('should contain websocketJavaPreset as second element', () => { | ||
| const websocketPreset = JavaModelsPresets[1]; | ||
| expect(websocketPreset).toBeDefined(); | ||
| expect(websocketPreset.class).toBeDefined(); | ||
| expect(websocketPreset.enum).toBeDefined(); | ||
| expect(websocketPreset.union).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('websocketJavaPreset class.self', () => { | ||
| const websocketPreset = JavaModelsPresets[1]; | ||
|
|
||
| it('should add package and basic imports for class without Map properties', () => { | ||
| const mockModel = { | ||
| properties: { | ||
| prop1: { property: { type: 'String' } }, | ||
| prop2: { property: { type: 'Integer' } } | ||
| } | ||
| }; | ||
| const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel }); | ||
|
|
||
| expect(result).toContain(PACKAGE_DECLARATION); | ||
| expect(result).toContain(IMPORT_OBJECTS); | ||
| expect(result).toContain(TEST_CLASS_CONTENT); | ||
| expect(result).not.toContain(IMPORT_MAP); | ||
| }); | ||
|
|
||
| it('should add Map import when property type is Map<String, Object>', () => { | ||
| const mockModel = { | ||
| properties: { | ||
| prop1: { property: { type: MAP_STRING_OBJECT_TYPE } }, | ||
| prop2: { property: { type: 'String' } } | ||
| } | ||
| }; | ||
| const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel }); | ||
|
|
||
| expect(result).toContain(PACKAGE_DECLARATION); | ||
| expect(result).toContain(IMPORT_OBJECTS); | ||
| expect(result).toContain(IMPORT_MAP); | ||
| expect(result).toContain(TEST_CLASS_CONTENT); | ||
| }); | ||
|
|
||
| it('should add Map import only once for multiple Map properties', () => { | ||
| const mockModel = { | ||
| properties: { | ||
| prop1: { property: { type: MAP_STRING_OBJECT_TYPE } }, | ||
| prop2: { property: { type: MAP_STRING_OBJECT_TYPE } }, | ||
| prop3: { property: { type: MAP_STRING_OBJECT_TYPE } } | ||
| } | ||
| }; | ||
| const result = websocketPreset.class.self({ content: TEST_CLASS_CONTENT, model: mockModel }); | ||
|
|
||
| const mapImportCount = (result.match(/import java\.util\.Map;/g) || []).length; | ||
| expect(mapImportCount).toBe(1); | ||
| }); | ||
|
|
||
| it('should handle model with no properties', () => { | ||
| const mockModel = { properties: {} }; | ||
| const content = 'public class EmptyClass {}'; | ||
| const result = websocketPreset.class.self({ content, model: mockModel }); | ||
|
|
||
| expect(result).toContain(PACKAGE_DECLARATION); | ||
| expect(result).toContain(IMPORT_OBJECTS); | ||
| expect(result).toContain('public class EmptyClass {}'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('websocketJavaPreset enum.self', () => { | ||
| const websocketPreset = JavaModelsPresets[1]; | ||
|
|
||
| it('should add package declaration to enum content', () => { | ||
| const content = 'public enum Status { ACTIVE, INACTIVE }'; | ||
| const result = websocketPreset.enum.self({ content }); | ||
|
|
||
| expect(result).toBe(`${PACKAGE_WITH_NEWLINES}public enum Status { ACTIVE, INACTIVE }`); | ||
| }); | ||
|
|
||
| it('should handle empty enum content', () => { | ||
| const content = ''; | ||
| const result = websocketPreset.enum.self({ content }); | ||
|
|
||
| expect(result).toBe(PACKAGE_WITH_NEWLINES); | ||
| }); | ||
| }); | ||
|
|
||
| describe('websocketJavaPreset union.self', () => { | ||
| const websocketPreset = JavaModelsPresets[1]; | ||
|
|
||
| it('should add package declaration to union content', () => { | ||
| const content = 'public class UnionType {}'; | ||
| const result = websocketPreset.union.self({ content }); | ||
|
|
||
| expect(result).toBe(`${PACKAGE_WITH_NEWLINES}public class UnionType {}`); | ||
| }); | ||
|
|
||
| it('should handle empty union content', () => { | ||
| const content = ''; | ||
| const result = websocketPreset.union.self({ content }); | ||
|
|
||
| expect(result).toBe(PACKAGE_WITH_NEWLINES); | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.