Code Monkey home page Code Monkey logo

Comments (6)

patrickberkeley avatar patrickberkeley commented on May 13, 2024 1

✅ understood and fixed. Thank you.

from miragejs.

samselikoff avatar samselikoff commented on May 13, 2024

It's still basically impossible to truly subclass arrays in JS which is why we chose to go the route of exposing the models as a true array via models. I usually recommend collection.models.[arrayMethod] as an escape hatch. That being said we do have filter/sort/slice on there so I'm in favor of adding additional methods.

(One problem we ran into in the past was folks getting confused Collections were actual true arrays, so they'd reasonably pass them into things like lodash array functions only to hit very strange and confusing errors. So that's a concern too.)

What's the use case you ran into? Wanting to do something like [...collection]?

from miragejs.

patrickberkeley avatar patrickberkeley commented on May 13, 2024

The use case is nested components:

The parent model has a has-many relationship, that gets passed into an outer component e.g.,

App code

Context consuming outer component

<Outer @book={{model}}>

Outer component template

<Inner @chapters={{book.chapters}}>

Inner component JS

@computed('chapters.[]')
get transformedData() {
  return this.chapters.toArray().map(...);
}

Test code

this.book = this.server.create('book');
this.chapters = this.server.createList('chapter', 3, { book });

The tests blow up because we have passed books.chapters down into the child and called toArray() and map() on it in the JS file. For app code this works because they are in the store, but test code errors because chapters mirage Collection doesn't have all the array methods.

from miragejs.

samselikoff avatar samselikoff commented on May 13, 2024

Mmm, I see what's happened. You should not be doing this with Mirage - Mirage models are really intended for the "server" and shouldn't be passed to Ember components.

Give this a read (the second called "Creating Ember Data models on the client with Mirage") and let me know if it makes sense: https://www.ember-cli-mirage.com/docs/testing/integration-and-unit-tests#creating-ember-data-models-on-the-client-with-mirage

from miragejs.

timbakkum avatar timbakkum commented on May 13, 2024

Forgive me for commenting on a closed issue, but I love mirage and i'm using it to generate fixtures for my unit test input but wondering what the best way is to get the raw data (array of objects in this case) without the extra mirage functions tagged on like find that overwrite the existing array methods.

I've come so far:

mockserver.createList('fileRaw', 10);
const files: FileData[] = Array.from(mockserver.schema.db.fileRaws);
const result = someFn(files);
// assert on result

Is there a better way? I found that Array.from(mockserver.db['fileRaws']) also works but it is not intuitive to do this as an inexperienced mirage users in my team.

from miragejs.

samselikoff avatar samselikoff commented on May 13, 2024

If you want to get everything in the database try ‘server.db.dump’. This will give you the data in Mirage’s raw db format (camelCase, foreign keys and such). If you want to go through your serializer layer try ‘server.serialize(modelOrCollection).

from miragejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.