I am trying to write a test using Jest. There is already a similar test testing if an attribute that is calculated gives a tooltip saying so. Now I am trying to test if an attribute that has an inline reference gives a tooltip saying so. There is added "hasInlineRef: false" to the AnalysisList. It has to be false, else it will overwrite the previous test. But when initializing it for my test, I would like to change it so hasInlineRef = true, which should make the test pass. I suspect it is my very limited knowledge of JavaScript that fails me here.
describe("AnalysisList", () => {
const analysisList: Array<AnalysisListItem> = [{
value: "AAAAAA",
count: 23456,
capped: false,
hasInlineRef: false,
dbValue: "AAAAAA"
}, {
value: "AA",
count: 800,
capped: true,
hasInlineRef: false,
dbValue: "AA"
}];
Below I would like to initialize the AnalysisList with hasInlineRef = true.
test("should have inline reference attribute tooltip when attribute is hasInlineRef", () => {
const component = mount(<AnalysisList analysisList={analysisList} onSearch={jest.fn()} />);
const tooltip = component.find("Tooltip").first().simulate("click");
expect(tooltip.props().title).toContain("inlineRefAttributesNoSearch");
});
Answering question "how to change single property inside the array?" has straightforward answer:
Here we can see accessing array's item by its index(
[0]
) and accessing property by its namehasInlineRef
.Need to do the same for all items in array makes us either using loops:
Or using methods available for any Array:
More on working with arrays in JS: https://www.w3schools.com/js/js_arrays.asp
Thanks to accessing by reference original
analysisList
changes as well as we transform any of its item one-by-one.But. If we look at the very start point I believe it's bad idea to mutate test data per-test case. Why?
If you don't like having huge data payload in test's code you may move it into separate
data_with_some_nuances.json
,another_data_payload.json
and load them into test with the help ofimport
statement.