ngxs-labs / firestore-plugin

Firestore plugin for NGXS
https://ngxs-firebase-plugin.netlify.com/
MIT License
20 stars 14 forks source link

How to handle unit tests? #48

Closed jorgemarcondes closed 1 year ago

jorgemarcondes commented 3 years ago

Have some example how to mock at unit tests?

joaqcid commented 3 years ago

hi @jorgemarcondes what kind of test are you trying to write?

BL-marcusrogers commented 2 years ago

@joaqcid Fantastic library, thanks for all you've done with it.

For unit tests, it would be nice to be able to do something like: store.dispatch(new MyStateUpdate('x')); // called from the component/service you're testing, of course. expect(store.selectSnapshot(MyState)).toEqual('x'); // check the store to make sure it's been updated as expected.

Doing this on a state object not backed by Firestore works, but I can't seem to get it to run when Firestore is involved. Maybe I'm just not mocking AngularFirestore correctly. Here's what I've tried so far:

export const angularFirestoreMock = { doc: (...ignore: any) => ({ ref: { withConverter: (...ignore: any) => ({}), }, set: (_d: any) => new Promise((resolve, _reject) => resolve()) }), };

I've looked through the tests used in your package, but can't seem to get the formula right. Any ideas would be appreciated.

joaqcid commented 2 years ago

Hi @BL-marcusrogers

Assuming you have something like this in your @State

this.ngxsFirestoreConnect.connect(YOURACTION, {
  to: ({ payload }) => this.YOUR_FIRESTORE_SERVICE.doc$(payload)
});

You should provide YOUR_FIRESTORE_SERVICE and mock doc$ or collection$ depending on what you need. Your mock could look something like this:

mockFIRESTORE_SERVICE = jest.fn(() => ({
  collection$: new BehaviorSubject([{ raceId: 'a' }])
}));

let me know if this helps, if you want a complete example, take a look to this file