ExB 1.9 Unit Test Advice

264
1
09-28-2022 04:09 PM
dreamfool
New Contributor II

I'm new to writing unit tests and try to get some inspirations from "show-unit-tests" from the SDK, but "Query Features" is not working for me, and would like your input on both the syntax and conceptual design of the test.

I have created a custom map widget that access coordinates from another widget and draw a point (graphic in graphic layer) on the map.

The coordinates in the other widgets are passed in with code:

this.props.dispatch(appActions.widgetStatePropChange(this.props.id, 'X', value))
this.props.dispatch(appActions.widgetStatePropChange(this.props.id, 'Y', value))

The coordinates are also shown as text above the map in the format of:

"X: <value> Y: <value>"

I am thinking for the unit test to be valuable, I should:
 
1) test the access from widgetsState
mock 2 values and try to access them, then assert their values?
 
2) test the API:
continue from #1, make a graphic in graphic layer, then ???
 

 

import { React, getAppStore, appActions } from 'jimu-core'
import _Widget from '../src/runtime/widget'
import { widgetRender, wrapWidget, getInitState, getDefaultAppConfig } from 'jimu-for-test'

const render = widgetRender()

jest.mock('jimu-core', () => {
  return {
    ...jest.requireActual('jimu-core'),
    loadArcGISJSAPIModule: jest.fn().mockImplementation(moduleId => {
      let module
      if (moduleId === 'esri/layers/GraphicLayer') {
        module = jest.fn().mockImplementation(() => {
          return {
            //not sure what goes in here
          }
        })
      }
      return Promise.resolve(module)
    })
  }
})

describe('test map-view widget', () => {
  it('test label, map from state', () => {
    const Widget = wrapWidget(_Widget, { config: {} })

    getAppStore().dispatch(appActions.updateStoreState(getInitState().merge({
      appConfig: getDefaultAppConfig().merge({
        widgets: {
          w1: {
            label: 123 // not working
          }
        }
      })
    })))

    const renderResult = render(<Widget widgetId="w1" />)
    expect(renderResult.queryByText('123')).toBeInTheDocument()
  })

  it('test api', async () => {
    const { findByText } = render(<Widget widgetId="w2" />)
    // Not sure what goes in here
  })
})

 

1 Reply
MorganLancaster
New Contributor

Hello,

Did you ever find a resolution to this issue?
It would be really helpful learning.

 

Kind Regards,

Morgan

0 Kudos