Okay, for the first thing, then do either
export function sortEvents (events) { // your logic
or
const sortEvents = (events) => {
then afterwards
export sortEvents;
The second thing – this is a misunderstanding of what classes are, in any language, not just JS. They aren’t just a way to collect some functions under a name on a file, they’re a way of defining a factory that creates objects:
class MyClass {
myMethod() {
return "hello"
}
}
myMethod
doesn’t exist as a separate thing, and doesn’t exist at all unless the class is instantiated:
const example = new MyClass()
example.myMethod() // returns "hello"
You can’t access myMethod
separately.
And even if you could (which, just to emphasise, is not possible in any language), what would this.props.events
be? If myMethod
could be called as a separate function, then this
is going to be called in a scope where this
is the global scope (so as these tests are being run in Node, is global
). There is no property called props
on global
, so the test cannot work: outside of that instantiated class, there is no data for the function to work on. Classes encapsulate some data and some functionality to work on that data.
So to unit test that function, you need to export it as a separate function, and you need to give it a parameter of events
.
In the test, you pass it a piece of data that you write – an array of objects that represents events
– and you check that it correctly sorts that. The test is checking that, given data that is the same shape as that which will be used in reality, that that unit of functionality works as expected.
In the class, you call that function, and pass it this.props.events
.
Also
The import statement is not destructuring (although it looks similar), it is just the syntax for named imports:
export function someFunction() {}
const foo = 1;
export foo;
export class SomeClass {}
They are all named exports, so to import:
import { someFunction, foo, SomeClass } from "./someFile";
You only get to miss off the curly brackets if you are importing a default export (or under a namespace, but I’ll not confuse things further):
export default foo;
import foo from "./somefile";