npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-data-tiny-factory

v0.0.4

Published

An EmberCLI addon for creating mock Ember Data models to use in your unit tests.

Downloads

7

Readme

Ember Data Tiny Factory - (EmberCLI Addon) Build Status

This is an EmberCLI addon that allows you to create mock Ember Data models for use in your unit tests.

  • Works with Mocha and QUnit.
  • Automatic setup/teardown of ModelFactory before/after each test...
  • Automatic lookup of transitive dependencies (DS.Transforms and DS.Model) in your models. (You do NOT need to register models that you use with ModelFactory in your test's 'needs' relationship)
  • Supports both template-based record creation, and ad-hoc definition of id or properties for a record.
  • Allows generation of factory templates using ember g factory [MODEL_NAME]

Installation

  • Install: ember install git+ssh://[email protected]:grese/ember-data-tiny-factory.git
    • If you see this error: "The ember generate command requires an entity name to be specified. For more details, use ember help.", run the following setup command:
    • Setup: ember g ember-data-tiny-factory tiny-factory (skip if you did not see the error above)
  • After setup, have a look in your tests/test-helper.js file:
    • You should see that the following lines of code have been automaticaly added to the file (if QUnit, the import statement will say 'ember-data-tiny-factory/setup/qunit').
      // ...
      import { factorySetup } from 'ember-data-tiny-factory/setup/mocha';
      
      // ...
      factorySetup({
          modulePrefix: '[YOUR_APP_NAME]',
          resolver: resolver
      });
    • If you do not see these lines, just add them (with the correct import statement for your test framework).
      • Mocha: import { factorySetup } from 'ember-data-tiny-factory/setup/mocha';
      • QUnit: import { factorySetup } from 'ember-data-tiny-factory/setup/qunit';

Usage

Factories

To create records based on a particular model, you'll need to create a 'factory' for that model. The name of your factory needs to map to the name of the model it represents. (For instance, a 'post' model would have a 'post' factory).

  • Create a Factory named 'post':

    • ember g factory post
    • This will create a factory in tests/factories/post.js, which will have the following code:
      import ModelFactory from 'ember-data-tiny-factory';
      
      ModelFactory.define('post', {
          index: {}
      });
  • Defining 'templates' in the factory

    • 'index' template will be used by default.
    • Some sample templates...
      // in tests/factories/post.js ...
      ModelFactory.define('post', {
          index: {
            title: 'The index template will be used by default...',
            date: '2015-07-04'
          },
          noTitle: {
              title: null,
              date: '2015-07-04'
          },
          noDate: {
              title: 'I have no date...',
              date: null
          }
      });
      
      // in tests/factories/comment.js ...
      ModelFactory.define('post', {
          index: {
              comment: 'Well, that was SOME story!',
              date: '2015-07-04',
              isApproved: false
          },
          approved: {
              comment: 'Well, that was SOME story!',
              date: '2015-07-04',
              isApproved: false
          },
          empty: {
              comment: 'Well, that was SOME story!',
              date: '2015-07-04',
              isApproved: false
          }
      });
    • The ModelFactory will automatically generate IDs for your records if you do not specify one. For the most part, it is recommended that you do NOT specify the 'id' property in your templates. You do actually have the ability to specify the 'id' in your templates, but be mindful that you will not be able to create lists of records from that template if it has an 'id' hard-coded because each EmberData record must have a unique 'id'.
  • Creating a record, or a list of records

    • createRecord:

      
      ModelFactory.createRecord(
          "MODEL_NAME",
          ("TEMPLATE_NAME" | {CUSTOM_OBJECT}),
          ("CUSTOM_ID")
      )
    • createRecordList:

      
      ModelFactory.createRecordList(
          "MODEL_NAME",
          COUNT,
          ("TEMPLATE_NAME" | [TEMPLATE_NAMES_ARRAY] | [CUSTOM_OBJECTS_ARRAY] | [MIXED_ARRAY]),
          ([CUSTOM_IDS_ARRAY])
      )
    • Create record(s) from a template:

      // ...
      import ModelFactory from 'ember-data-tiny-factory';
      // ...
      
      // Creates a 'post' with 'index' template...
      var post = ModelFactory.createRecord('post');
      // Creates a post with 'noDate' template...
      var noDatePost = ModelFactory.createRecord('post', 'noDate');
      // Creates a post with 'noTitle' template...
      var noTitlePost = ModelFactory.createRecord('post', 'noTitle');
      
      // Creates a list of 3 comments with 'index' template...
      var comments = ModelFactory.createRecordList('comment', 3);
      // Creates a list of 10 comments with 'approved' template...
      var approvedComments = ModelFactory.createRecordList('comment', 10, 'approved');
      // Creates a list of comments: one 'index', one 'approved', one 'empty'...
      var assortedComments = ModelFactory.createRecordList('comment', 2, ['index', 'approved', 'empty']);
      
      // NOTE: All of the records created in the examples above will have an automatically generated 'id'.
    • Create record(s) from a template with a custom 'id'

      // ...
      import ModelFactory from 'ember-data-tiny-factory';
      // ...
      
      // Create a 'post' with 'index' template, and 'id' of '12345'.
      var post = ModelFactory.createRecord('post', 'index', '12345');
      
      // Create a 'post' with 'index' template, and 'id' of '23456'.
      var post2 = ModelFactory.createRecord('post', null, '23456');
      
      // Create a list of 'comments' with 'index' template, and specific 'id's.
      var comments = ModelFactory.createRecord('comment', 3, null, [
          '1', '2', '3'
      ]);
      
      // Create a list of 'comments' with 'empty' template, and specific 'id's.
      var ids = ['101', '102', '103'];
      var comments2 = ModelFactory.createRecord('comment', 3, 'empty', ids);
    • Create ad-hoc record(s) with custom properties

      // ...
      import ModelFactory from 'ember-data-tiny-factory';
      // ...
      
      // Create a custom post...
      var customPost = ModelFactory.createRecord('post', {
          id: '10',
          title: 'I am a custom post',
          date: '2015-07-04'
      });
      
      // Create 3 custom comments (no template)...
      var customComments = ModelFactory.createRecordList('comment', 3, [
          {id: '1', comment: 'splendid!'},
          {id: '2', comment: 'splendid!'},
          {comment: 'fantastic!'}, // (will have auto-generated ID)
      ]);
      customPost.set('comments', customComments);
      
      // Create a mixed list of templated comments, and custom comments...
      var mixedComments = ModelFactory.createRecordList('comment', 5, [
          'empty',
          'approved',
          {id: '2', comment: 'splendid!'},
          'index',
          {id: '3', comment: 'wonderful!!'}
      ]);
      
      // Create custom post with auto-generated ID:
      var customPostAutoId = ModelFactory.createRecord('post', {
          title: 'I will have an auto-generated id',
          date: '2015-07-04'
      });

Collaboration

Installation

Running

  • ember server
  • Visit your app at http://localhost:4200.

Running Tests

  • ember test
  • ember test --server

Building