user91
user91

Reputation: 125

Type Error: cannot read property ' map ' of undefined

I am try to run an unit test for the following code here is where the error is pointing to:

populateActionDropdown = (actionList) => {
    let actions = actionList.map(action => {
        if (action.Name != 'Sign Out') {
            let layout = ''
            if (action.Command.includes('Layout')) {
                if (/\(([^)]+)\)/.exec(action.Command)) {
                    layout = '/' + /\(([^)]+)\)/.exec(action.Command)[1].toLowerCase();
                }
            }

This is how I have been setting up most of my unit test. But I'm having problems calling props that contains map or sort next to it. Example : this.props.something.map() or props.sort(). How could i mock actionList to make sure map gets called

Thanks

Unit test code below :

describe('ActionMenu ', () => {

    let tree;
    let baseProps;
    let mockaction = {
        actionId: "test",
        name: "test",
        map: "test",
    };
    let mocksetTarget;
    let mocksetAction;
    let mockmodule = {
        name: "test module name",
    };
    let mockactionMenuDropdownOpen;
    let mockactionMenuToggle;
    let mockactionList;

    beforeEach(() => {
        baseProps = {
            action: mockaction,
            setTarget: mocksetTarget,
            setAction: mocksetAction,
            module: mockmodule,
            actionMenuDropdownOpen: mockactionMenuDropdownOpen,
            actionMenuToggle: mockactionMenuToggle,
            actionList: mockactionList,
        }
    })

    it(' Should render with all of the props', () => {
        tree = renderer.create(<BrowserRouter><ActionMenuComponent {...baseProps}
        />
        </BrowserRouter>)
        let treeJson = tree.toJSON()
        expect(treeJson).toMatchSnapshot();
        tree.unmount()
    });
});

Upvotes: 0

Views: 4198

Answers (1)

Ana Liza Pandac
Ana Liza Pandac

Reputation: 4871

But I'm having problems calling props that contains map or sort next to it. Example : this.props.something.map() or props.sort().

To make your test pass, assign the following value to your mockactionList:

let mockactionList = [
  {
    actionId: "test",
    Name: "test",
    Command: []
  },
  {
    actionId: "test2",
    Name: "test2",
    Command: []
  }
];

Upvotes: 1

Related Questions