Writing a test case for a condition where the data is an array that generates many data using a function

Viewed 19

This is the function that is in the mockData folder


export const generateNotificationMockMany = (count) => (
    [...new Array(count)].map((index) => ({
            'group': `group number ${index}`,
            'message': `msg for group number ${index}`,
            'datetime': '',
        }),
    ));




this is the test case I've been trying to write. the data is being imported from a file that has this function in it that generates the mockdata. How can I write the test case for it? basically how can I use a loop to be able to get access to all the generated data by the function?

it('renders properly for the many variant', ()=> {
        render(<NotificationMenu notifications={notificationsMock}/>)

    });

1 Answers

Does one of the following options solve your problem, or do you expect something else ?

loop over test cases:

const mocks = generateNotificationMockMany(10);

for (let notificationsMock of mocks) {
  it(`renders properly for the variant ${notificationsMock}`, ()=> {
    render(<NotificationMenu notifications={notificationsMock}/>)
  });
}

loop inside test case:

const mocks = generateNotificationMockMany(10);

it(`renders properly for the many variant`, ()=> {
  for (let notificationsMock of mocks) {
    render(<NotificationMenu notifications={notificationsMock}/>)
  }
});
Related