import { WNode , Constructor , MetaBase , WidgetMetaConstructor , MiddlewareResultFactory , DefaultMiddlewareResult } from '@dojo/framework/core/interfaces' ; import { CustomComparator , harness } from '@dojo/framework/testing/harness/harness' ; import { SinonStub } from 'sinon' ; import { WidgetBase } from '@dojo/framework/core/WidgetBase' ; import { createResourceTemplate } from '@dojo/framework/core/middleware/resources' ; import { THEME_KEY } from '../../../middleware/theme' ; export const noop = ( ) => { } ; export const stubEvent = { stopPropagation : noop , preventDefault : noop , target : { } } ; export const isResourceComparator = ( value ) => Boolean ( value && value . template ) ; export const isObjectComparator = ( value ) => typeof value === 'object' ; export const isStringComparator = ( value ) => value === null || typeof value === 'string' ; export const isStringObjectComparator = ( value ) => Object . keys ( value ) . every ( ( key ) => value [ key ] === null || typeof value [ key ] === 'string' ) ; export const isFocusedComparator = ( value ) => value ( ) === true ; export const isNotFocusedComparator = ( value ) => value ( ) === false ; export const compareId = { selector : '*' , property : 'id' , comparator : isStringComparator } ; export const compareWidgetId = { selector : '*' , property : 'widgetId' , comparator : isStringComparator } ; export const compareForId = { selector : '*' , property : 'forId' , comparator : isStringComparator } ; export const compareAria = { selector : '*' , property : 'aria' , comparator : isStringObjectComparator } ; export const compareAriaControls = { selector : '*' , property : 'aria-controls' , comparator : isStringComparator } ; export const compareAriaLabelledBy = { selector : '*' , property : 'aria-labelledby' , comparator : isStringComparator } ; export const compareAriaOwns = { selector : '*' , property : 'aria-owns' , comparator : isStringComparator } ; export const compareAriaDescribedBy = { selector : '*' , property : 'aria-describedby' , comparator : isStringComparator } ; export const compareLabelId = { selector : '*' , property : 'labelId' , comparator : isStringComparator } ; interface HarnessOptions { customComparator ? : CustomComparator [ ] ; middleware ? : [ MiddlewareResultFactory < any , any , any , any > , ( ) => DefaultMiddlewareResult ] [ ] ; } export const createHarness = ( globalCompares ) => { return ( renderFunction , options = [ ] ) => { if ( Array . isArray ( options ) ) { return harness ( renderFunction , [ ... globalCompares , ... options ] ) ; } else { return harness ( renderFunction , { ... options , customComparator : [ ... globalCompares , ... ( options . customComparator || [ ] ) ] } ) ; } } ; } ; export function MockMetaMixin < T extends Constructor < WidgetBase < any > > > ( Base , mockStub ) { return class extends Base { protected meta < T extends MetaBase > ( MetaType ) { return mockStub ( MetaType ) ; } } ; } export const compareTheme = { selector : '*' , property : 'theme' , comparator : isObjectComparator } ; export const compareResource = { selector : '*' , property : 'resource' , comparator : isResourceComparator } ; export function createTestResource ( data , id , options ? , transform ? ) { return { template : { id : 'test' , template : createResourceTemplate < any > ( id ) , initOptions : { id : 'test' , data } , transform } , options } ; } /**
 * Creates a theme object from css objects using the theme key
 */ export const createTestTheme = ( ... classes ) => { const theme = { } ; classes . forEach ( ( classesParam ) => { const { [ THEME_KEY ] : key , ... classNames } = classesParam ; if ( key ) { theme [ key ] = classNames ; } else { Object . assign ( theme , classNames ) ; } } ) ; return theme ; } ;