Skip to content

Commit

Permalink
Merge pull request #1501 from lucasnetau/newtests
Browse files Browse the repository at this point in the history
Improve test coverage for tinymce, quill, select, and custom controls
  • Loading branch information
lucasnetau committed Jan 17, 2024
2 parents 23f09c8 + 8173976 commit e5b724b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@
"collectCoverage": true,
"coverageDirectory": ".jest/coverage",
"coveragePathIgnorePatterns": [
"tests/"
"tests/",
"<rootDir>/src/js/control/index.js"
],
"testEnvironment": "jsdom",
"testEnvironmentOptions": {
Expand Down
2 changes: 1 addition & 1 deletion src/js/control/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default class controlCustom extends control {
*/
build() {
let custom = this.template
/* istanbul ignore next */
if (!custom) {
/* istanbul ignore next */
return control.error(
`Invalid custom control type '${this.type}'. Please ensure you have registered it correctly as a template option.`,
)
Expand Down
20 changes: 10 additions & 10 deletions src/js/customControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ export default class customControls {
const value = typeof i18n == 'object' ? i18n[lookupCamel] || i18n[type] : i18n
if (value) {
return value
} else {
// otherwise check the mi18n object - allow for mapping a lookup to a custom mi18n lookup
let mapped = def.mi18n
if (typeof mapped === 'object') {
mapped = mapped[lookupCamel] || mapped[type]
}
if (!mapped) {
mapped = lookupCamel
}
return mi18n.get(mapped)
}

// otherwise check the mi18n object - allow for mapping a lookup to a custom mi18n lookup
let mapped = def.mi18n
if (typeof mapped === 'object') {
mapped = mapped[lookupCamel] || mapped[type]
}
if (!mapped) {
mapped = lookupCamel
}
return mi18n.get(mapped)
}

get definition() {
Expand Down
1 change: 1 addition & 0 deletions src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
function createNewEvent(eventName) {
let event
/* istanbul ignore else - historical InternetExplorer only */
if (typeof Event === 'function') {
event = new Event(eventName)
} else {
Expand Down
38 changes: 38 additions & 0 deletions tests/control/custom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,42 @@ describe('Test Custom Control', () => {
fbWrap.formRender({formData, templates})
expect(fbWrap.find('span')[0].textContent).toBe('not preview')
})

test('test loading js/css from custom template', async () => {
const fbWrap = $('<div>')
const cbOnRender = jest.fn()

const formData = [
{
'type': 'starRating',
'required': false,
'label': 'Star Rating',
'name': 'starRating-1697591966052-0'
},
]
const templates = {
starRating: function(fieldData) {
const control = this
control.field = `<span id="${fieldData.name}">`
return {
field: control.field,
onRender: () => {
$(control.field).rateYo( {rating: 2} )
cbOnRender()
},
js: 'https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js',
css: 'https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.css'
}
}
}

fbWrap.formRender({formData, templates})

await new Promise(resolve => setTimeout(resolve, 2000))

expect(cbOnRender.mock.calls).toHaveLength(1)

expect(fbWrap.find('#starRating-1697591966052-0')[0].outerHTML).toBe('<span id="starRating-1697591966052-0"></span>')

})
})
34 changes: 34 additions & 0 deletions tests/control/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,38 @@ describe('Test Text Control', () => {
element.value = 'option-1'
expect(element.validity.valid).toBeTruthy()
})

test('userData overrides default selections', async () => {
const controlInstance = new controlSelect({
'type': 'select',
'required': true,
'label': 'Select',
'className': 'form-control',
'placeholder': 'Select an option...',
'name': 'test-select',
'multiple': false,
'values': [
{
'label': 'Option 1',
'value': 'option-1',
'selected': true
},
{
'label': 'Option 2',
'value': 'option-2',
'selected': false
},
{
'label': 'Option 3',
'value': 'option-3',
'selected': false
}
],
userData: ['option-2']
}, false)

const element = controlInstance.build()
controlInstance.onRender()
expect(element.value).toBe('option-2')
})
})
35 changes: 33 additions & 2 deletions tests/control/textarea.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import controlTextarea from '../../src/js/control/textarea.js'
import controlTinymce from '../../src/js/control/textarea.tinymce'
import controlQuill from '../../src/js/control/textarea.quill'
import { getScripts, getStyles, isCached } from '../../src/js/utils'

const sleep = milliseconds => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}

const loadResources = async (js, css) => {
if (css) {
getStyles(css)
}
if (js && !isCached(js)) {
await getScripts(js)
}
}

describe('Test Text Control', () => {
test('test building control element', async () => {
const controlInstance = new controlTextarea({
Expand Down Expand Up @@ -35,7 +50,7 @@ describe('Test Text Control', () => {
})

describe('Test building text variations and subtypes', () => {
test('can render TinyMCE', () => {
test('can render TinyMCE', async () => {
const controlInstance = new controlTinymce({
'type': 'textarea',
'required': false,
Expand All @@ -45,12 +60,22 @@ describe('Test building text variations and subtypes', () => {
'access': false,
'subtype': 'tinymce',
}, false)
controlInstance.configure()
const element = controlInstance.build()
expect(element.constructor.name).toBe('HTMLTextAreaElement')
expect(element.type).toBe('textarea')

window.document.body.appendChild(element) //Element must be attached to dom for tinymce theme to work otherwise exception thrown

await loadResources(controlInstance.js, controlInstance.css)

expect(window.tinymce).not.toBeUndefined()
controlInstance.onRender()
await sleep(2000)
expect(window.tinymce.get('tinymce-elem')).not.toBeNull()
})

test('can render Quill', () => {
test('can render Quill', async () => {
const controlInstance = new controlQuill({
'type': 'textarea',
'required': false,
Expand All @@ -60,7 +85,13 @@ describe('Test building text variations and subtypes', () => {
'access': false,
'subtype': 'quill',
}, false)
controlInstance.configure()
const element = controlInstance.build()
expect(element.constructor.name).toBe('HTMLDivElement')

window.document.body.appendChild(element) //Element must be attached to dom for quill to work otherwise exception thrown

await loadResources(controlInstance.js, controlInstance.css)
controlInstance.onRender()
})
})

0 comments on commit e5b724b

Please sign in to comment.