0

In my react/jest unit test I am trying to call a static method called getSortedAppColumns(), this is part of the component:

static getSortedAppColumns = (appColumns, sortBy, criticalFirst) => {
...
return { id, applications: applicationsSorted, ...rest };
});
};

When I run this test:

it('should sort', () => {
    const presets = { presets: { sortCriteria: 'name' } };
    const component = (
      <AppTrackerContainer applicationStages={applicationStages} {...presets} />
    );
    const wrapper = shallow(component);
    const instance = wrapper.instance();
    const appList = applicationStages[0].applications;
    expect(instance.getSortedAppColumns(appList)).toMatchSnapshot();
});

Of course I have this error:

TypeError: instance.getSortedAppColumns is not a function

  45 |     const instance = wrapper.instance();
  46 |     const appList = applicationStages[0].applications;
> 47 |     expect(instance.getSortedAppColumns(appList)).toMatchSnapshot();

Obviously a static method is not an instance but how can I utilise this static call in my jest snapshottest? Need to have a snapshot test that is the bottom line.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
bier hier
  • 20,970
  • 42
  • 97
  • 166
  • Hm.. Because it's not an instance method, but static one? – dfsq Feb 11 '19 at 08:32
  • 1
    You can't access static members directly on the instance *in general*, this isn't anything to do with Jest (they're on the prototype). But if the static method is just a helper for the rest of what the component is doing, I wouldn't test it directly anyway; test the overall *behaviour* not your current implementation. – jonrsharpe Feb 11 '19 at 08:34

1 Answers1

2

AppTrackerContainer.getSortedAppColumns(appList)

js call static method from class

nubinub
  • 1,898
  • 2
  • 15
  • 23