adopted-ember-addons / ember-data-factory-guy

Factories and helper functions for (unit, integration, acceptance) testing + development scenarios with Ember Data
MIT License
301 stars 136 forks source link

modelName being used as payloadKey #465

Open Techn1x opened 2 years ago

Techn1x commented 2 years ago

I have a model, adapter and serializer for a library-book, which are are all jsonapi

library-book/
- model.ts
- adapter.ts
- serializer.ts

Nothing weird so far. However, the API that my application talks to, sets the type / payload key as book. In order for the above to talk to the API correctly, my serializer adds the library- prefix on incoming payloads (via modelNameFromPayloadKey), and strips the prefix on outgoing requests (via payloadKeyFromModelName)

This all works great against the real API, but I've been having challenges getting that to work with factory guy.

Consider the following example

const json = buildList('library-book', { name: 'a' }, { name: 'b' }).add({ meta: { page: 1 } })
mockQuery('library-book').returns({ json })

When running a test that calls store.query('library-book', { page: 1 }) and debugging the incoming payload, it seems the type key (payload key) is set to 'library-book' when I need it to be 'book'. I would have thought that factory guy would call payloadKeyFromModelName from the serializer to figure out the necessary payload key to use, but my debugging shows that it doesn't seem to be called.

A quick search on this repo brings up this, which seems to indicate that payloadKeyFromModelName should be called when serializeMode is true - but I don't really know what that means or if I am able to set that easily. https://github.com/adopted-ember-addons/ember-data-factory-guy/blob/160aec825ef73f085b40eca9868f988f05389d69/addon/converter/jsonapi-fixture-converter.js#L15-L26

Any help appreciated, thanks! (Even if the answer is "you can't do that", it'll save me digging into this further)