-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(textarea): add tests about odsChange emit on first render
- Loading branch information
Showing
4 changed files
with
182 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
67 changes: 67 additions & 0 deletions
67
packages/ods/src/components/textarea/tests/controller/ods-textarea.spec.ts
This file contains 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,67 @@ | ||
jest.mock('../../../../utils/dom'); | ||
|
||
import { setInternalsValidityFromHtmlElement } from '../../../../utils/dom'; | ||
import { getInitialValue, updateInternals } from '../../src/controller/ods-textarea'; | ||
|
||
describe('ods-textarea controller', () => { | ||
beforeEach(jest.clearAllMocks); | ||
|
||
describe('getInitialValue', () => { | ||
it('should return null if value is null and no default value', () => { | ||
expect(getInitialValue(null)).toBe(null); | ||
}); | ||
|
||
it('should return string if value is set regarding of default value', () => { | ||
expect(getInitialValue('')).toBe(''); | ||
expect(getInitialValue('value')).toBe('value'); | ||
expect(getInitialValue('value', 'default')).toBe('value'); | ||
// @ts-ignore for test purpose | ||
expect(getInitialValue('value', null)).toBe('value'); | ||
}); | ||
|
||
it('should return default value if value is null', () => { | ||
expect(getInitialValue(null, '')).toBe(''); | ||
expect(getInitialValue(null, 'default')).toBe('default'); | ||
}); | ||
}); | ||
|
||
describe('updateInternals', () => { | ||
const dummyInternal = { | ||
setFormValue: jest.fn(), | ||
} as unknown as ElementInternals; | ||
const dummyTextarea = { dummy: 'textarea' }; | ||
|
||
it('should set internal value with empty string', () => { | ||
// @ts-ignore for test purpose | ||
updateInternals(dummyInternal); | ||
expect(dummyInternal.setFormValue).toHaveBeenCalledWith(''); | ||
|
||
// @ts-ignore for test purpose | ||
updateInternals(dummyInternal, undefined, {} as HTMLTextAreaElement); | ||
expect(dummyInternal.setFormValue).toHaveBeenCalledWith(''); | ||
|
||
updateInternals(dummyInternal, null, {} as HTMLTextAreaElement); | ||
expect(dummyInternal.setFormValue).toHaveBeenCalledWith(''); | ||
}); | ||
|
||
it('should set internal value with string value', () => { | ||
const dummyValue = 'dummy value'; | ||
|
||
updateInternals(dummyInternal, dummyValue, {} as HTMLTextAreaElement); | ||
|
||
expect(dummyInternal.setFormValue).toHaveBeenCalledWith(dummyValue); | ||
}); | ||
|
||
it('should not set internal validity if no textarea element is defined', () => { | ||
updateInternals(dummyInternal, 'dummyValue'); | ||
|
||
expect(setInternalsValidityFromHtmlElement).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should set internal validity if textarea element is defined', () => { | ||
updateInternals(dummyInternal, 'dummyValue', dummyTextarea as unknown as HTMLTextAreaElement); | ||
|
||
expect(setInternalsValidityFromHtmlElement).toHaveBeenCalledWith(dummyTextarea, dummyInternal); | ||
}); | ||
}); | ||
}); |