Modifier

Partager via


Module test file

This article covers the module test file in Microsoft Dynamics 365 Commerce.

The module test file is used for local unit testing. It contains the mock data that is required to run the tests.

Example

The following example shows a default test file that is created for a new module.

import { buildMockModuleProps} from '@msdyn365-commerce/core';
/// <reference types="jest" />

// tslint:disable-next-line:no-unused-variable
import * as React from 'react';
import * as renderer from 'react-test-renderer';

import ProductFeature from '../productFeature';
import { IProductFeatureData } from '../productFeature.data';
import {
    IProductFeatureConfig,
    IProductFeatureProps
} from '../productFeature.props.autogenerated';

const mockData: IProductFeatureData = {
    actionResponse: {
        text: 'Sample Response Data'
  }
};

const mockConfig: IProductFeatureConfig = {
    showText: 'productFeature'
};

const mockActions = {};

describe('ProductFeature', () => {
    let moduleProps: IProductFeatureProps<IProductFeatureData>;
    beforeAll(() => {
        moduleProps = buildMockModuleProps(mockData, mockActions, mockConfig) as IProductFeatureProps<IProductFeatureData>;
    });
    it('renders correctly', () => {
        const component: renderer.ReactTestRenderer = renderer.create(
            <ProductFeature {...moduleProps} />
        );
        const tree: renderer.ReactTestRendererJSON | null = component.toJSON();
        expect(tree).toMatchSnapshot();
    });
});

Note that the mock data fields are set inside this file.

Additional resources

Modules overview

Module definition file

Module React component file

Module view file

Module data file

Module mock file

Module props.autogenerated.ts file