简体   繁体   中英

Meaning of the done() function

Let us suppose you have the following action

export const startAddExpense = (expenseData = {}) => {
  return (dispatch) => {
    const {
      description = '',
      note = '',
      amount = 0,
      createdAt = 0
    } = expenseData;
    const expense = { description, note, amount, createdAt };

    return database.ref('expenses').push(expense).then((ref) => {
      dispatch(addExpense({
        id: ref.key,
        ...expense
      }));
    });
  };
};

I am storing expenses data to a database(firebase in my case) and then I dispatch the addExpense function to the store

And here the test for it.

test('should add expense to database and store', (done) => {
    const store = createMockStore({});
    const expenseData = {
      description: 'Mouse',
      amount: 3000,
      note: 'This one is better',
      createdAt: 1000
    };

    store.dispatch(startAddExpense(expenseData)).then(() => {
      expect(1).toBe(1);
      done();
    });
});

What is the meaning of the done() function? Does it mean the callback function should wait until the insertion of data in the firebase database has been completed?

Thanks, Theo.

done mean that your test function has asynchronous code. So you should manually call done when you get an async result. Also, async tests have third parameters that define timeout after that function would be considered as failed. https://jasmine.github.io/tutorials/async#callbacks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM