├── .circleci └── config.yml ├── .eslintrc.json ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── karma.conf.js ├── lib ├── assert.js ├── association.js ├── db-collection.js ├── db.js ├── factory.js ├── faker.js ├── identity-manager.js ├── index.js ├── orm │ ├── associations │ │ ├── association.js │ │ ├── belongs-to.js │ │ └── has-many.js │ ├── collection.js │ ├── model.js │ ├── polymorphic-collection.js │ └── schema.js ├── response.js ├── route-handler.js ├── route-handlers │ ├── base.js │ ├── function.js │ ├── object.js │ └── shorthands │ │ ├── base.js │ │ ├── delete.js │ │ ├── get.js │ │ ├── head.js │ │ ├── post.js │ │ └── put.js ├── serializer-registry.js ├── serializer.js ├── serializers │ ├── active-model-serializer.js │ ├── json-api-serializer.js │ └── rest-serializer.js ├── server.js ├── trait.js └── utils │ ├── ember-shims.js │ ├── extend.js │ ├── inflector.js │ ├── is-association.js │ ├── normalize-name.js │ ├── reference-sort.js │ └── uuid.js ├── package.json ├── rollup.config.js ├── tests ├── .eslintrc.json ├── index.js ├── integration │ ├── database-test.js │ ├── db │ │ └── identity-manager-test.js │ ├── factories │ │ ├── after-create-test.js │ │ └── helpers-test.js │ ├── http-verbs-test.js │ ├── load-fixtures-test.js │ ├── orm │ │ ├── all-test.js │ │ ├── attrs-test.js │ │ ├── belongs-to │ │ │ ├── 1-basic │ │ │ │ ├── _helper.js │ │ │ │ ├── _regressions-test.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 10-one-to-one-polymorphic │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 2-named │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 3-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 4-named-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 5-named-reflexive-explicit-inverse │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 6-one-way-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 7-named-one-way-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 8-one-to-one │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 9-one-way-polymorphic │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-id-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── misc-test.js │ │ │ ├── notes.md │ │ │ └── regressions │ │ │ │ └── issue-1112-test.js │ │ ├── collection-inflector-test.js │ │ ├── collection-test.js │ │ ├── create-test.js │ │ ├── destroy-test.js │ │ ├── find-test.js │ │ ├── first-test.js │ │ ├── has-many │ │ │ ├── 1-basic │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 10-many-to-many-polymorphic │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 2-named │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 3-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── new-test.js │ │ │ ├── 4-named-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── new-test.js │ │ │ ├── 5-named-reflexive-explicit-inverse │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── new-test.js │ │ │ ├── 6-one-way-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── new-test.js │ │ │ ├── 7-named-one-way-reflexive │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── new-test.js │ │ │ ├── 8-many-to-many │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── new-test.js │ │ │ └── 9-one-way-polymorphic │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ ├── mixed │ │ │ ├── 1-one-to-many │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ └── instantiating-test.js │ │ │ ├── 2-many-to-one │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ └── instantiating-test.js │ │ │ └── 3-one-to-many-polymorphic │ │ │ │ ├── _helper.js │ │ │ │ ├── accessor-test.js │ │ │ │ ├── association-create-test.js │ │ │ │ ├── association-new-test.js │ │ │ │ ├── association-set-ids-test.js │ │ │ │ ├── association-set-test.js │ │ │ │ ├── create-test.js │ │ │ │ ├── delete-test.js │ │ │ │ └── instantiating-test.js │ │ ├── reinitialize-associations-test.js │ │ ├── schema-verification │ │ │ ├── belongs-to-test.js │ │ │ ├── has-many-test.js │ │ │ └── mixed-test.js │ │ ├── update-test.js │ │ └── where-test.js │ ├── passthrough-test.js │ ├── route-handlers │ │ ├── delete-shorthand-test.js │ │ ├── function-handler-test.js │ │ ├── get-shorthand-test.js │ │ ├── head-shorthand-test.js │ │ ├── post-shorthand-test.js │ │ └── put-shorthand-test.js │ ├── serializers │ │ ├── active-model-serializer-test.js │ │ ├── base │ │ │ ├── associations │ │ │ │ ├── embedded-collection-test.js │ │ │ │ ├── embedded-model-test.js │ │ │ │ ├── many-to-many-test.js │ │ │ │ ├── polymorphic-belongs-to-test.js │ │ │ │ ├── polymorphic-has-many-test.js │ │ │ │ ├── sideloading-assorted-collections-test.js │ │ │ │ ├── sideloading-collection-test.js │ │ │ │ └── sideloading-model-test.js │ │ │ ├── assorted-collections-test.js │ │ │ ├── attribute-key-formatting-test.js │ │ │ ├── attrs-test.js │ │ │ ├── basic-test.js │ │ │ ├── full-request-test.js │ │ │ ├── override-serialize-test.js │ │ │ ├── root-test.js │ │ │ ├── serialize-array-of-models-test.js │ │ │ └── serialize-ids-test.js │ │ ├── json-api-serializer │ │ │ ├── associations │ │ │ │ ├── collection-test.js │ │ │ │ ├── includes-test.js │ │ │ │ ├── key-for-relationship-test.js │ │ │ │ ├── links-test.js │ │ │ │ ├── many-to-many-test.js │ │ │ │ ├── model-test.js │ │ │ │ └── polymorphic-test.js │ │ │ ├── attrs-test.js │ │ │ ├── base-test.js │ │ │ └── key-formatting-test.js │ │ ├── rest-serializer-test.js │ │ └── schema-helper.js │ ├── server-config-test.js │ ├── server-with-orm-test.js │ └── server │ │ ├── custom-function-handler-test.js │ │ ├── customized-normalize-method-test.js │ │ ├── factory-creation-test.js │ │ ├── get-full-path-test.js │ │ ├── resource-shorthand-test.js │ │ └── shorthand-sanity-test.js ├── jquery-shim.js ├── qunit-shim.js └── unit │ ├── collection-test.js │ ├── controller-test.js │ ├── db-test.js │ ├── db │ └── identity-manager-test.js │ ├── factory-test.js │ ├── faker-test.js │ ├── inflector-test.js │ ├── model-test.js │ ├── reference-sort-test.js │ ├── response-test.js │ ├── route-handlers │ └── shorthands │ │ └── base-test.js │ ├── schema-test.js │ ├── serializers │ ├── active-model-serializer-test.js │ └── rest-serializer-test.js │ ├── server-test.js │ └── utils │ └── normalize-name-test.js └── yarn.lock /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "standard", 3 | "rules": { 4 | "semi": [ 5 | "error", 6 | "always" 7 | ], 8 | "space-before-function-paren": [ 9 | "error", 10 | { 11 | "anonymous": "never", 12 | "named": "never", 13 | "asyncArrow": "always" 14 | } 15 | ], 16 | "indent": [ 17 | "error", 18 | 2 19 | ] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /package-lock.json 3 | /yarn.lock 4 | /dist 5 | /yarn-error.log 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 BigTest 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # :warning: DEPRECATED :warning: 2 | In order to make BigTest development faster and friction free, we've consolidated all of our individual projects into a [single repository on the Frontside Organization](https://github.com/thefrontside/bigtest). We'd love to see you there! 3 | 4 | _note: the last release from this repository was 0.0.1_ 5 | 6 | ## Mirage Server 7 | 8 | A client-side server to develop, test and prototype your app. 9 | 10 | > Note: This repository has been archived in favor of https://github.com/miragejs/server 11 | > Documentation and tutorials at https://miragejs.com/docs 12 | 13 | This project is a plain vanilla javascript extraction of the 14 | [ember-cli-mirage][1] project. It can be used inside of any framework, 15 | including React. The goal is for it to be eventually used upstream 16 | by Ember mirage proper. 17 | 18 | ## Usage 19 | 20 | ``` javascript 21 | import Mirage, { Factory } from '@bigtest/mirage'; 22 | 23 | let server = new Mirage({ 24 | environment: 'test', 25 | factories: { 26 | address: Factory 27 | } 28 | }); 29 | 30 | // do some stuff with the server. 31 | 32 | // stop intercepting requests 33 | server.shutdown() 34 | ``` 35 | 36 | ## Development 37 | 38 | Test suite is running in QUnit and Karma. 39 | 40 | ``` 41 | $ yarn 42 | $ yarn start // karma server 43 | $ yarn test // single run 44 | ``` 45 | 46 | [1]: http://www.ember-cli-mirage.com/ 47 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = function(config) { 4 | config.set({ 5 | frameworks: ['qunit', 'jquery-2.1.4'], 6 | reporters: ['mocha'], 7 | browsers: ['Chrome'], 8 | 9 | files: [ 10 | 'tests/index.js' 11 | ], 12 | 13 | preprocessors: { 14 | 'tests/index.js': ['webpack'] 15 | }, 16 | 17 | // enable the browser UI 18 | client: { 19 | clearContext: false, 20 | qunit: { 21 | showUI: true, 22 | testTimeout: 5000 23 | } 24 | }, 25 | 26 | webpack: { 27 | resolve: { 28 | alias: { 29 | qunit: path.resolve(__dirname, 'tests/qunit-shim.js'), 30 | jquery: path.resolve(__dirname, 'tests/jquery-shim.js'), 31 | '@bigtest/mirage': path.resolve(__dirname, 'lib/index.js') 32 | } 33 | }, 34 | 35 | module: { 36 | rules: [ 37 | { 38 | test: /\.js$/, 39 | exclude: /node_modules/, 40 | use: [{ 41 | loader: 'babel-loader', 42 | options: { 43 | presets: ['@babel/env'] 44 | } 45 | }] 46 | } 47 | ] 48 | } 49 | }, 50 | 51 | // webpack-dev-middleware config 52 | webpackMiddleware: { 53 | stats: 'errors-only' 54 | }, 55 | 56 | // enable our plugins 57 | plugins: [ 58 | require('karma-qunit'), 59 | require('karma-jquery'), 60 | require('karma-webpack'), 61 | require('karma-chrome-launcher'), 62 | require('karma-mocha-reporter') 63 | ] 64 | }); 65 | }; 66 | -------------------------------------------------------------------------------- /lib/assert.js: -------------------------------------------------------------------------------- 1 | /* eslint no-console: 0 */ 2 | let errorProps = [ 3 | 'description', 4 | 'fileName', 5 | 'lineNumber', 6 | 'message', 7 | 'name', 8 | 'number', 9 | 'stack' 10 | ]; 11 | 12 | export default function assert(bool, text) { 13 | if (typeof bool === 'string' && !text) { 14 | throw new MirageError(bool); 15 | } 16 | 17 | if (!bool) { 18 | throw new MirageError(text.replace(/^ +/gm, '') || 'Assertion failed'); 19 | } 20 | } 21 | 22 | /** 23 | @public 24 | Copied from ember-metal/error 25 | */ 26 | export function MirageError() { 27 | let tmp = Error.apply(this, arguments); 28 | 29 | for (let idx = 0; idx < errorProps.length; idx++) { 30 | let prop = errorProps[idx]; 31 | 32 | if (['description', 'message', 'stack'].indexOf(prop) > -1) { 33 | this[prop] = `Mirage: ${tmp[prop]}`; 34 | } else { 35 | this[prop] = tmp[prop]; 36 | } 37 | } 38 | 39 | console.error(this.message); 40 | console.error(this); 41 | } 42 | 43 | MirageError.prototype = Object.create(Error.prototype); 44 | -------------------------------------------------------------------------------- /lib/association.js: -------------------------------------------------------------------------------- 1 | let association = function(...traitsAndOverrides) { 2 | let __isAssociation__ = true; 3 | return { 4 | __isAssociation__, 5 | traitsAndOverrides 6 | }; 7 | }; 8 | 9 | export default association; 10 | -------------------------------------------------------------------------------- /lib/faker.js: -------------------------------------------------------------------------------- 1 | import faker from 'faker'; 2 | 3 | let list = { 4 | random() { 5 | let items = arguments.length > 0 ? arguments : []; 6 | 7 | return function() { 8 | return faker.random.arrayElement(items); 9 | }; 10 | }, 11 | 12 | cycle() { 13 | let items = arguments.length > 0 ? arguments : []; 14 | 15 | return function(i) { 16 | return items[i % items.length]; 17 | }; 18 | } 19 | }; 20 | 21 | faker.list = list; 22 | 23 | faker.random.number.range = function(min, max) { 24 | return function(/* i */) { 25 | return Math.random() * (max - min) + min; 26 | }; 27 | }; 28 | 29 | export default faker; 30 | -------------------------------------------------------------------------------- /lib/identity-manager.js: -------------------------------------------------------------------------------- 1 | function isNumber(n) { 2 | return (+n).toString() === n.toString(); 3 | } 4 | 5 | /** 6 | * IdentityManager for a DbCollection 7 | * @class 8 | * @constructor 9 | * @public 10 | */ 11 | class IdentityManager { 12 | constructor() { 13 | this._nextId = 1; 14 | this._ids = {}; 15 | } 16 | 17 | /** 18 | * @method get 19 | * @private 20 | */ 21 | get() { 22 | return this._nextId; 23 | } 24 | 25 | /** 26 | * @method set 27 | * @param {String|Number} n 28 | * @public 29 | */ 30 | set(n) { 31 | if (this._ids[n]) { 32 | throw new Error(`Attempting to use the ID ${n}, but it's already been used`); 33 | } 34 | 35 | if (isNumber(n) && +n >= this._nextId) { 36 | this._nextId = +n + 1; 37 | } 38 | 39 | this._ids[n] = true; 40 | } 41 | 42 | /** 43 | * @method inc 44 | * @private 45 | */ 46 | inc() { 47 | let nextValue = this.get() + 1; 48 | 49 | this._nextId = nextValue; 50 | 51 | return nextValue; 52 | } 53 | 54 | /** 55 | * @method fetch 56 | * @return {String} Unique identifier 57 | * @public 58 | */ 59 | fetch() { 60 | let id = this.get(); 61 | 62 | this._ids[id] = true; 63 | 64 | this.inc(); 65 | 66 | return id.toString(); 67 | } 68 | 69 | /** 70 | * @method reset 71 | * @public 72 | */ 73 | reset() { 74 | this._nextId = 1; 75 | this._ids = {}; 76 | } 77 | } 78 | 79 | export default IdentityManager; 80 | -------------------------------------------------------------------------------- /lib/orm/associations/association.js: -------------------------------------------------------------------------------- 1 | import { dasherize } from '@bigtest/mirage'; 2 | 3 | export default class Association { 4 | constructor(modelName, opts) { 5 | if (typeof modelName === 'object') { 6 | // Received opts only 7 | this.modelName = undefined; 8 | this.opts = modelName; 9 | } else { 10 | // The modelName of the association. (Might not be passed in - set later 11 | // by schema). 12 | this.modelName = modelName ? dasherize(modelName) : ''; 13 | this.opts = opts || {}; 14 | } 15 | 16 | // The key pointing to the association 17 | this.key = ''; 18 | 19 | // The modelName that owns this association 20 | this.ownerModelName = ''; 21 | } 22 | 23 | /** 24 | * A setter for schema, since we don't have a reference at constuction time. 25 | * 26 | * @method setSchema 27 | * @public 28 | */ 29 | setSchema(schema) { 30 | this.schema = schema; 31 | } 32 | 33 | /** 34 | * Returns true if the association is reflexive. 35 | * 36 | * @method isReflexive 37 | * @return {Boolean} 38 | * @public 39 | */ 40 | isReflexive() { 41 | let isExplicitReflexive = !!(this.modelName === this.ownerModelName && this.opts.inverse); 42 | let isImplicitReflexive = !!(this.opts.inverse === undefined && this.ownerModelName === this.modelName); 43 | 44 | return isExplicitReflexive || isImplicitReflexive; 45 | } 46 | 47 | get isPolymorphic() { 48 | return this.opts.polymorphic; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/response.js: -------------------------------------------------------------------------------- 1 | export default class Response { 2 | constructor(code, headers = {}, data = {}) { 3 | this.code = code; 4 | this.headers = headers; 5 | this.data = data; 6 | } 7 | 8 | toRackResponse() { 9 | let { headers } = this; 10 | if (!headers.hasOwnProperty('Content-Type')) { 11 | headers['Content-Type'] = 'application/json'; 12 | } 13 | 14 | return [this.code, this.headers, this.data]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lib/route-handlers/function.js: -------------------------------------------------------------------------------- 1 | import BaseRouteHandler from './base'; 2 | 3 | export default class FunctionRouteHandler extends BaseRouteHandler { 4 | constructor(schema, serializerOrRegistry, userFunction, path) { 5 | super(); 6 | this.schema = schema; 7 | this.serializerOrRegistry = serializerOrRegistry; 8 | this.userFunction = userFunction; 9 | this.path = path; 10 | } 11 | 12 | handle(request) { 13 | return this.userFunction(this.schema, request); 14 | } 15 | 16 | setRequest(request) { 17 | this.request = request; 18 | } 19 | 20 | serialize(response, serializerType) { 21 | let serializer; 22 | 23 | if (serializerType) { 24 | serializer = this.serializerOrRegistry.serializerFor(serializerType, { explicit: true }); 25 | } else { 26 | serializer = this.serializerOrRegistry; 27 | } 28 | 29 | return serializer.serialize(response, this.request); 30 | } 31 | 32 | normalizedRequestAttrs() { 33 | let { 34 | path, 35 | request, 36 | request: { requestHeaders } 37 | } = this; 38 | 39 | let modelName = this.getModelClassFromPath(path); 40 | 41 | if (/x-www-form-urlencoded/.test(requestHeaders['Content-Type'])) { 42 | return this._getAttrsForFormRequest(request); 43 | } else { 44 | return this._getAttrsForRequest(request, modelName); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /lib/route-handlers/object.js: -------------------------------------------------------------------------------- 1 | export default class ObjectRouteHandler { 2 | constructor(schema, serializerOrRegistry, object) { 3 | this.schema = schema; 4 | this.serializerOrRegistry = serializerOrRegistry; 5 | this.object = object; 6 | } 7 | 8 | handle(/* request */) { 9 | return this.object; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /lib/route-handlers/shorthands/base.js: -------------------------------------------------------------------------------- 1 | import { toCollectionName } from '../../utils/normalize-name'; 2 | import BaseRouteHandler from '../base'; 3 | 4 | export default class BaseShorthandRouteHandler extends BaseRouteHandler { 5 | constructor(schema, serializerOrRegistry, shorthand, path, options = {}) { 6 | super(); 7 | shorthand = shorthand || this.getModelClassFromPath(path); 8 | this.schema = schema; 9 | this.serializerOrRegistry = serializerOrRegistry; 10 | this.shorthand = shorthand; 11 | this.options = options; 12 | 13 | let type = Array.isArray(shorthand) ? 'array' : typeof shorthand; 14 | if (type === 'string') { 15 | let modelClass = this.schema[toCollectionName(shorthand)]; 16 | this.handle = (request) => { 17 | return this.handleStringShorthand(request, modelClass); 18 | }; 19 | } else if (type === 'array') { 20 | let modelClasses = shorthand.map((modelName) => this.schema[toCollectionName(modelName)]); 21 | this.handle = (request) => { 22 | return this.handleArrayShorthand(request, modelClasses); 23 | }; 24 | } 25 | } 26 | 27 | // handleStringShorthand() { 28 | // 29 | // } 30 | // 31 | // handleArrayShorthand() { 32 | // 33 | // } 34 | } 35 | -------------------------------------------------------------------------------- /lib/route-handlers/shorthands/delete.js: -------------------------------------------------------------------------------- 1 | import assert from '../../assert'; 2 | import BaseShorthandRouteHandler from './base'; 3 | import { pluralize, camelize } from '@bigtest/mirage'; 4 | 5 | export default class DeleteShorthandRouteHandler extends BaseShorthandRouteHandler { 6 | /* 7 | Remove the model from the db of type *camelizedModelName*. 8 | 9 | This would remove the user with id :id: 10 | Ex: this.del('/contacts/:id', 'user'); 11 | */ 12 | handleStringShorthand(request, modelClass) { 13 | let modelName = this.shorthand; 14 | let camelizedModelName = camelize(modelName); 15 | assert( 16 | modelClass, 17 | `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist. Create it using 'ember g mirage-model ${modelName}'.` 18 | ); 19 | 20 | let id = this._getIdForRequest(request); 21 | return modelClass.find(id).destroy(); 22 | } 23 | 24 | /* 25 | Remove the model and child related models from the db. 26 | 27 | This would remove the contact with id `:id`, as well 28 | as this contact's addresses and phone numbers. 29 | Ex: this.del('/contacts/:id', ['contact', 'addresses', 'numbers'); 30 | */ 31 | handleArrayShorthand(request, modelClasses) { 32 | let id = this._getIdForRequest(request); 33 | 34 | let parent = modelClasses[0].find(id); 35 | let childTypes = modelClasses.slice(1) 36 | .map((modelClass) => pluralize(modelClass.camelizedModelName)); 37 | 38 | // Delete related children 39 | childTypes.forEach((type) => parent[type].destroy()); 40 | parent.destroy(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /lib/route-handlers/shorthands/head.js: -------------------------------------------------------------------------------- 1 | import assert from '../../assert'; 2 | import BaseShorthandRouteHandler from './base'; 3 | import { Response, camelize } from '@bigtest/mirage'; 4 | 5 | export default class HeadShorthandRouteHandler extends BaseShorthandRouteHandler { 6 | /* 7 | Retrieve a model/collection from the db. 8 | 9 | Examples: 10 | this.head('/contacts', 'contact'); 11 | this.head('/contacts/:id', 'contact'); 12 | */ 13 | handleStringShorthand(request, modelClass) { 14 | let modelName = this.shorthand; 15 | let camelizedModelName = camelize(modelName); 16 | 17 | assert( 18 | modelClass, 19 | `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist. Create it using 'ember g mirage-model ${modelName}'.` 20 | ); 21 | 22 | let id = this._getIdForRequest(request); 23 | if (id) { 24 | let model = modelClass.find(id); 25 | if (!model) { 26 | return new Response(404); 27 | } else { 28 | return new Response(204); 29 | } 30 | } else if (this.options.coalesce && request.queryParams && request.queryParams.ids) { 31 | let model = modelClass.find(request.queryParams.ids); 32 | 33 | if (!model) { 34 | return new Response(404); 35 | } else { 36 | return new Response(204); 37 | } 38 | } else { 39 | return new Response(204); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /lib/route-handlers/shorthands/post.js: -------------------------------------------------------------------------------- 1 | import assert from '../../assert'; 2 | import BaseShorthandRouteHandler from './base'; 3 | import { camelize } from '@bigtest/mirage'; 4 | 5 | export default class PostShorthandRouteHandler extends BaseShorthandRouteHandler { 6 | /* 7 | Push a new model of type *camelizedModelName* to the db. 8 | 9 | For example, this will push a 'user': 10 | this.post('/contacts', 'user'); 11 | */ 12 | 13 | handleStringShorthand(request, modelClass) { 14 | let modelName = this.shorthand; 15 | let camelizedModelName = camelize(modelName); 16 | assert( 17 | modelClass, 18 | `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist. Create it using 'ember g mirage-model ${modelName}'.` 19 | ); 20 | 21 | let attrs = this._getAttrsForRequest(request, modelClass.camelizedModelName); 22 | return modelClass.create(attrs); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /lib/route-handlers/shorthands/put.js: -------------------------------------------------------------------------------- 1 | import assert from '../../assert'; 2 | import BaseShorthandRouteHandler from './base'; 3 | import { camelize } from '@bigtest/mirage'; 4 | 5 | export default class PutShorthandRouteHandler extends BaseShorthandRouteHandler { 6 | /* 7 | Update an object from the db, specifying the type. 8 | 9 | this.put('/contacts/:id', 'user'); 10 | */ 11 | handleStringShorthand(request, modelClass) { 12 | let modelName = this.shorthand; 13 | let camelizedModelName = camelize(modelName); 14 | 15 | assert( 16 | modelClass, 17 | `The route handler for ${request.url} is trying to access the ${camelizedModelName} model, but that model doesn't exist. Create it using 'ember g mirage-model ${modelName}'.` 18 | ); 19 | 20 | let id = this._getIdForRequest(request); 21 | let attrs = this._getAttrsForRequest(request, modelClass.camelizedModelName); 22 | 23 | return modelClass.find(id).update(attrs); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lib/serializers/active-model-serializer.js: -------------------------------------------------------------------------------- 1 | import Serializer from '../serializer'; 2 | import { underscore, pluralize, dasherize, singularize } from '@bigtest/mirage'; 3 | 4 | export default class ActiveModelSerializer extends Serializer { 5 | keyForModel(type) { 6 | return underscore(type); 7 | } 8 | 9 | keyForAttribute(attr) { 10 | return underscore(attr); 11 | } 12 | 13 | keyForRelationship(type) { 14 | return pluralize(underscore(type)); 15 | } 16 | 17 | keyForEmbeddedRelationship(attributeName) { 18 | return underscore(attributeName); 19 | } 20 | 21 | keyForRelationshipIds(type) { 22 | return `${underscore(singularize(type))}_ids`; 23 | } 24 | 25 | keyForForeignKey(relationshipName) { 26 | return `${underscore(relationshipName)}_id`; 27 | } 28 | 29 | keyForPolymorphicForeignKeyId(relationshipName) { 30 | return `${underscore(relationshipName)}_id`; 31 | } 32 | 33 | keyForPolymorphicForeignKeyType(relationshipName) { 34 | return `${underscore(relationshipName)}_type`; 35 | } 36 | 37 | normalize(payload) { 38 | let type = Object.keys(payload)[0]; 39 | let attrs = payload[type]; 40 | 41 | let jsonApiPayload = { 42 | data: { 43 | type: pluralize(type), 44 | attributes: {} 45 | } 46 | }; 47 | if (attrs.id) { 48 | jsonApiPayload.data.id = attrs.id; 49 | } 50 | Object.keys(attrs).forEach((key) => { 51 | if (key !== 'id') { 52 | jsonApiPayload.data.attributes[dasherize(key)] = attrs[key]; 53 | } 54 | }); 55 | 56 | return jsonApiPayload; 57 | } 58 | }; 59 | -------------------------------------------------------------------------------- /lib/serializers/rest-serializer.js: -------------------------------------------------------------------------------- 1 | import ActiveModelSerializer from './active-model-serializer'; 2 | import { camelize, singularize, pluralize } from '@bigtest/mirage'; 3 | 4 | export default class RestSerializer extends ActiveModelSerializer { 5 | keyForModel(type) { 6 | return camelize(type); 7 | } 8 | 9 | keyForAttribute(attr) { 10 | return camelize(attr); 11 | } 12 | 13 | keyForRelationship(type) { 14 | return camelize(pluralize(type)); 15 | } 16 | 17 | keyForEmbeddedRelationship(attributeName) { 18 | return camelize(attributeName); 19 | } 20 | 21 | keyForRelationshipIds(type) { 22 | return camelize(pluralize(type)); 23 | } 24 | 25 | keyForForeignKey(relationshipName) { 26 | return camelize(singularize(relationshipName)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /lib/trait.js: -------------------------------------------------------------------------------- 1 | let trait = function(extension) { 2 | let __isTrait__ = true; 3 | return { 4 | extension, 5 | __isTrait__ 6 | }; 7 | }; 8 | 9 | export default trait; 10 | -------------------------------------------------------------------------------- /lib/utils/extend.js: -------------------------------------------------------------------------------- 1 | import _assign from 'lodash/assign'; 2 | import _has from 'lodash/has'; 3 | 4 | export default function(protoProps, staticProps) { 5 | let Parent = this; 6 | 7 | class Child extends Parent { 8 | constructor(...args) { 9 | super(...args); 10 | // The constructor function for the new subclass is optionally defined by you 11 | // in your `extend` definition 12 | if (protoProps && _has(protoProps, 'constructor')) { 13 | protoProps.constructor.call(this, ...args); 14 | } 15 | } 16 | } 17 | 18 | // Add static properties to the constructor function, if supplied. 19 | 20 | _assign(Child, Parent, staticProps); 21 | 22 | // Add prototype properties (instance properties) to the subclass, 23 | // if supplied. 24 | if (protoProps) { 25 | _assign(Child.prototype, protoProps); 26 | } 27 | 28 | // Set a convenience property in case the parent's prototype is needed 29 | // later. 30 | Child.__super__ = Parent.prototype; 31 | 32 | return Child; 33 | } 34 | -------------------------------------------------------------------------------- /lib/utils/inflector.js: -------------------------------------------------------------------------------- 1 | import { 2 | underscore, 3 | camelize as _camelize, 4 | dasherize as _dasherize 5 | } from 'inflected'; 6 | 7 | export function camelize(word) { 8 | return _camelize(underscore(word), false); 9 | } 10 | 11 | export function dasherize(word) { 12 | return _dasherize(underscore(word)); 13 | } 14 | 15 | export { underscore }; 16 | export { singularize, pluralize, capitalize } from 'inflected'; 17 | -------------------------------------------------------------------------------- /lib/utils/is-association.js: -------------------------------------------------------------------------------- 1 | import _isPlainObject from 'lodash/isPlainObject'; 2 | 3 | export default function(object) { 4 | return _isPlainObject(object) && object.__isAssociation__ === true; 5 | } 6 | -------------------------------------------------------------------------------- /lib/utils/normalize-name.js: -------------------------------------------------------------------------------- 1 | import { 2 | camelize, 3 | pluralize, 4 | singularize, 5 | dasherize 6 | } from '@bigtest/mirage'; 7 | 8 | export function toCollectionName(type) { 9 | let modelName = dasherize(type); 10 | return camelize(pluralize(modelName)); 11 | } 12 | 13 | export function toModelName(type) { 14 | let modelName = dasherize(type); 15 | return singularize(modelName); 16 | } 17 | -------------------------------------------------------------------------------- /lib/utils/reference-sort.js: -------------------------------------------------------------------------------- 1 | // jscs:disable disallowVar, requireArrayDestructuring 2 | import _uniq from 'lodash/uniq'; 3 | import _flatten from 'lodash/flatten'; 4 | 5 | export default function(edges) { 6 | let nodes = _uniq(_flatten(edges)); 7 | let cursor = nodes.length; 8 | let sorted = new Array(cursor); 9 | let visited = {}; 10 | let i = cursor; 11 | 12 | let visit = function(node, i, predecessors) { 13 | if (predecessors.indexOf(node) >= 0) { 14 | throw new Error(`Cyclic dependency in properties ${JSON.stringify(predecessors)}`); 15 | } 16 | 17 | if (visited[i]) { 18 | return; 19 | } else { 20 | visited[i] = true; 21 | } 22 | 23 | let outgoing = edges.filter(function(edge) { 24 | return edge && edge[0] === node; 25 | }); 26 | i = outgoing.length; 27 | if (i) { 28 | let preds = predecessors.concat(node); 29 | do { 30 | let pair = outgoing[--i]; 31 | let child = pair[1]; 32 | if (child) { 33 | visit(child, nodes.indexOf(child), preds); 34 | } 35 | } while (i); 36 | } 37 | 38 | sorted[--cursor] = node; 39 | }; 40 | 41 | while (i--) { 42 | if (!visited[i]) { 43 | visit(nodes[i], i, []); 44 | } 45 | } 46 | 47 | return sorted.reverse(); 48 | } 49 | -------------------------------------------------------------------------------- /lib/utils/uuid.js: -------------------------------------------------------------------------------- 1 | /* 2 | UUID generator 3 | */ 4 | export default function() { 5 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { 6 | var r = Math.random() * 16 | 0; 7 | var v = c === 'x' ? r : (r & 0x3 | 0x8); 8 | return v.toString(16); 9 | }); 10 | } 11 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import babel from 'rollup-plugin-babel'; 2 | import resolve from 'rollup-plugin-node-resolve'; 3 | import commonjs from 'rollup-plugin-commonjs'; 4 | 5 | export default { 6 | input: 'lib/index.js', 7 | output: { 8 | file: 'dist/index.js', 9 | format: 'umd', 10 | name: 'BigTest.Mirage' 11 | }, 12 | plugins: [ 13 | resolve(), 14 | commonjs(), 15 | babel({ 16 | babelrc: false, 17 | comments: false, 18 | presets: [ 19 | ['@babel/preset-env', { 20 | modules: false 21 | }] 22 | ] 23 | }) 24 | ] 25 | }; 26 | -------------------------------------------------------------------------------- /tests/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "no-return-assign": 0, 4 | "no-unused-expressions": 0, 5 | "standard/no-callback-literal": 0, 6 | "no-new": 0 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/index.js: -------------------------------------------------------------------------------- 1 | var testsContext = require.context('.', true, /-test$/); 2 | testsContext.keys().forEach(testsContext); 3 | -------------------------------------------------------------------------------- /tests/integration/database-test.js: -------------------------------------------------------------------------------- 1 | import {module, test} from 'qunit'; 2 | import { Server, Model, Factory } from '@bigtest/mirage'; 3 | 4 | module('Integration | Database', { 5 | beforeEach() { 6 | this.server = new Server({ 7 | environment: 'development', 8 | scenarios: { 9 | default() {} 10 | }, 11 | models: { 12 | author: Model 13 | }, 14 | factories: { 15 | author: Factory 16 | }, 17 | fixtures: { 18 | authors: [ 19 | { id: 1, name: 'Zelda' } 20 | ] 21 | } 22 | }); 23 | }, 24 | afterEach() { 25 | this.server.shutdown(); 26 | } 27 | }); 28 | 29 | test(`[regression] When loaded, fixture files correctly update the database's autoincrement id`, function(assert) { 30 | this.server.loadFixtures(); 31 | 32 | this.server.schema.authors.create({}); 33 | 34 | let { authors } = this.server.db; 35 | assert.equal(authors.length, 2); 36 | assert.deepEqual(authors.map((a) => a.id), ['1', '2']); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/factories/after-create-test.js: -------------------------------------------------------------------------------- 1 | import { module, test } from 'qunit'; 2 | import { Model, Factory, belongsTo, Server } from '@bigtest/mirage'; 3 | 4 | module('Integration | Server | Factories | afterCreate', { 5 | beforeEach() { 6 | this.server = new Server({ 7 | environment: 'test', 8 | models: { 9 | author: Model, 10 | post: Model.extend({ 11 | author: belongsTo() 12 | }), 13 | comment: Model.extend({ 14 | post: belongsTo() 15 | }) 16 | }, 17 | factories: { 18 | author: Factory.extend({ 19 | afterCreate(author, server) { 20 | author.update({ name: 'Sam' }); 21 | server.create('post', { author }); 22 | } 23 | }), 24 | post: Factory.extend({ 25 | title: 'Lorem ipsum', 26 | afterCreate(post, server) { 27 | server.create('comment', { post }); 28 | } 29 | }), 30 | comment: Factory.extend({ 31 | text: 'Yo soy el nino' 32 | }) 33 | } 34 | }); 35 | }, 36 | afterEach() { 37 | this.server.shutdown(); 38 | } 39 | }); 40 | 41 | test('it works for models', function(assert) { 42 | let author = this.server.create('author'); 43 | 44 | assert.equal(author.name, 'Sam'); 45 | assert.deepEqual(this.server.db.posts.length, 1); 46 | assert.deepEqual(this.server.db.posts[0], { id: '1', title: 'Lorem ipsum', authorId: '1' }); 47 | assert.deepEqual(this.server.db.comments.length, 1); 48 | assert.deepEqual(this.server.db.comments[0], { id: '1', text: 'Yo soy el nino', postId: '1' }); 49 | }); 50 | 51 | // test('it works for db records', function(assert) { 52 | // }); 53 | -------------------------------------------------------------------------------- /tests/integration/load-fixtures-test.js: -------------------------------------------------------------------------------- 1 | import {module, test} from 'qunit'; 2 | import Server from '@bigtest/mirage'; 3 | 4 | module('Integration | Server #loadFixtures', { 5 | beforeEach() { 6 | this.server = new Server({ 7 | environment: 'development', 8 | scenarios: { 9 | default() {} 10 | }, 11 | factories: { 12 | author: {}, 13 | post: {}, 14 | comment: {} 15 | }, 16 | fixtures: { 17 | authors: [ 18 | { id: 1, name: 'Zelda' }, 19 | { id: 2, name: 'Link' } 20 | ], 21 | posts: [ 22 | { id: 1, title: 'Lorem' }, 23 | { id: 2, title: 'Ipsum' } 24 | ], 25 | comments: [ 26 | { id: 1, title: 'Lorem' } 27 | ] 28 | } 29 | }); 30 | }, 31 | afterEach() { 32 | this.server.shutdown(); 33 | } 34 | }); 35 | 36 | test('it can load all fixtures in the map', function(assert) { 37 | this.server.loadFixtures(); 38 | 39 | assert.equal(this.server.db.authors.length, 2); 40 | assert.equal(this.server.db.posts.length, 2); 41 | assert.equal(this.server.db.comments.length, 1); 42 | }); 43 | 44 | test('it can load a single named fixture file', function(assert) { 45 | this.server.loadFixtures('authors'); 46 | 47 | assert.equal(this.server.db.authors.length, 2); 48 | assert.equal(this.server.db.posts.length, 0); 49 | assert.equal(this.server.db.comments.length, 0); 50 | }); 51 | 52 | test('it can load several named single fixtures', function(assert) { 53 | this.server.loadFixtures('authors', 'posts'); 54 | 55 | assert.equal(this.server.db.authors.length, 2); 56 | assert.equal(this.server.db.posts.length, 2); 57 | assert.equal(this.server.db.comments.length, 0); 58 | }); 59 | -------------------------------------------------------------------------------- /tests/integration/orm/all-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db, Collection } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | module('Integration | ORM | #all'); 5 | 6 | test('it can return all models', function(assert) { 7 | let db = new Db({ 8 | users: [ 9 | { id: 1, name: 'Link' }, 10 | { id: 2, name: 'Zelda' } 11 | ] 12 | }); 13 | let User = Model.extend(); 14 | let schema = new Schema(db, { 15 | user: User 16 | }); 17 | 18 | let users = schema.users.all(); 19 | assert.ok(users instanceof Collection, 'it returns a collection'); 20 | assert.ok(users.models[0] instanceof User, 'each member of the collection is a model'); 21 | assert.equal(users.models.length, 2); 22 | assert.deepEqual(users.models[1].attrs, { id: '2', name: 'Zelda' }); 23 | }); 24 | 25 | test('it returns an empty array when no models exist', function(assert) { 26 | let db = new Db({ users: [] }); 27 | 28 | let User = Model.extend(); 29 | let schema = new Schema(db, { 30 | user: User 31 | }); 32 | 33 | let users = schema.users.all(); 34 | 35 | assert.ok(users instanceof Collection, 'it returns a collection'); 36 | assert.equal(users.modelName, 'user', 'the collection knows its type'); 37 | assert.equal(users.models.length, 0); 38 | }); 39 | -------------------------------------------------------------------------------- /tests/integration/orm/attrs-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | var db, schema, User; 5 | module('Integration | ORM | attrs', { 6 | beforeEach() { 7 | db = new Db({ users: [ 8 | { id: 1, name: 'Link', evil: false } 9 | ] }); 10 | 11 | User = Model.extend(); 12 | schema = new Schema(db, { 13 | user: User 14 | }); 15 | } 16 | }); 17 | 18 | test('attrs returns the models attributes', function(assert) { 19 | let user = schema.users.find(1); 20 | 21 | assert.deepEqual(user.attrs, { id: '1', name: 'Link', evil: false }); 22 | }); 23 | 24 | test('attributes can be read via plain property access', function(assert) { 25 | let user = schema.users.find(1); 26 | 27 | assert.equal(user.name, 'Link'); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/_regressions-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db, belongsTo } from '@bigtest/mirage'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | regressions'); 5 | 6 | test('belongsTo accessors works when foreign key is present but falsy', function(assert) { 7 | let db = new Db({ 8 | posts: [ 9 | { id: 1, authorId: 0, name: 'some post' } 10 | ], 11 | authors: [ 12 | { id: 0, name: 'Foo' } 13 | ] 14 | }); 15 | 16 | let schema = new Schema(db, { 17 | author: Model.extend(), 18 | post: Model.extend({ 19 | author: belongsTo() 20 | }) 21 | }); 22 | 23 | let post = schema.posts.find(1); 24 | assert.equal(post.author.name, 'Foo'); 25 | }); 26 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ post, author ] = this.helper[state](); 16 | 17 | assert.deepEqual(post.author, author || null, 'the model reference is correct'); 18 | assert.equal(post.authorId, author ? author.id : null, 'the modelId reference is correct'); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ post ] = this.helper[state](); 16 | 17 | let ganon = post.createAuthor({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(post.author.attrs, ganon.attrs); 21 | assert.equal(post.authorId, ganon.id); 22 | assert.equal(this.helper.schema.posts.find(post.id).authorId, ganon.id, 'the child was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ post ] = this.helper[state](); 17 | 18 | let ganon = post.newAuthor({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(post.author, ganon); 22 | assert.equal(post.authorId, null); 23 | 24 | post.save(); 25 | 26 | assert.ok(ganon.id, 'saving the child persists the parent'); 27 | assert.equal(post.authorId, ganon.id, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ post ] = this.helper[state](); 16 | let savedAuthor = this.helper.savedParent(); 17 | 18 | post.authorId = savedAuthor.id; 19 | 20 | assert.equal(post.authorId, savedAuthor.id); 21 | assert.deepEqual(post.author.attrs, savedAuthor.attrs); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ post ] = this.helper[state](); 31 | 32 | post.authorId = null; 33 | 34 | assert.equal(post.authorId, null); 35 | assert.deepEqual(post.author, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ post ] = this.helper[state](); 16 | let savedAuthor = this.helper.savedParent(); 17 | 18 | post.author = savedAuthor; 19 | 20 | assert.equal(post.authorId, savedAuthor.id); 21 | assert.deepEqual(post.author, savedAuthor); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ post ] = this.helper[state](); 26 | let newAuthor = this.helper.newParent(); 27 | 28 | post.author = newAuthor; 29 | 30 | assert.equal(post.authorId, null); 31 | assert.deepEqual(post.author, newAuthor); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ post ] = this.helper[state](); 36 | 37 | post.author = null; 38 | 39 | assert.equal(post.authorId, null); 40 | assert.deepEqual(post.author, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/1-basic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Basic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ post, author ] = this.helper[state](); 13 | 14 | if (author) { 15 | author.destroy(); 16 | post.reload(); 17 | } 18 | 19 | assert.equal(post.authorId, null); 20 | assert.deepEqual(post.author, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/10-one-to-one-polymorphic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-to-one Polymorphic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ comment, post ] = this.helper[state](); 16 | 17 | // We use .attrs here because otherwise deepEqual goes on infinite recursive comparison 18 | if (post) { 19 | assert.deepEqual(comment.commentable.attrs, post.attrs, 'the model reference is correct'); 20 | assert.deepEqual(comment.commentableId, { type: 'post', id: post.id }, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(comment.commentable, null, 'the model reference is correct'); 23 | assert.equal(comment.commentableId, null, 'the modelId reference is correct'); 24 | } 25 | 26 | // If there's a post in this state, make sure the inverse association is correct 27 | if (post) { 28 | assert.deepEqual(post.comment.attrs, comment.attrs, 'the inverse model reference is correct'); 29 | assert.equal(post.commentId, comment.id, 'the inverse modelId reference is correct'); 30 | } 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/10-one-to-one-polymorphic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-to-one Polymorphic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ comment ] = this.helper[state](); 16 | 17 | let post = comment.createCommentable('post', { title: 'Lorem' }); 18 | 19 | assert.ok(post.id, 'the parent was persisted'); 20 | assert.deepEqual(comment.commentable.attrs, post.attrs); 21 | assert.deepEqual(post.comment.attrs, comment.attrs, 'the inverse was set'); 22 | assert.deepEqual(comment.commentableId, { type: 'post', id: post.id }); 23 | assert.deepEqual(this.helper.schema.comments.find(comment.id).commentableId, { type: 'post', id: post.id }, 'the comment was persisted'); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/10-one-to-one-polymorphic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-to-one Polymorphic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ comment ] = this.helper[state](); 17 | 18 | let post = comment.newCommentable('post', { age: 300 }); 19 | 20 | assert.ok(!post.id, 'the parent was not persisted'); 21 | assert.deepEqual(comment.commentable, post); 22 | assert.deepEqual(comment.commentableId, { type: 'post', id: undefined }); 23 | assert.deepEqual(post.comment, comment, 'the inverse was set'); 24 | assert.equal(post.commentId, comment.id); 25 | 26 | comment.save(); 27 | 28 | assert.ok(post.id, 'saving the child persists the parent'); 29 | assert.deepEqual(comment.commentableId, { type: 'post', id: post.id }, 'the childs fk was updated'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/10-one-to-one-polymorphic/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-to-one Polymorphic | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ comment ] = this.helper[state](); 16 | let post = this.helper.savedParent(); 17 | 18 | comment.commentableId = { type: 'post', id: post.id }; 19 | 20 | assert.deepEqual(comment.commentableId, { type: 'post', id: post.id }); 21 | assert.deepEqual(comment.commentable.attrs, post.attrs); 22 | 23 | comment.save(); 24 | post.reload(); 25 | 26 | assert.equal(post.commentId, comment.id, 'the inverse was set'); 27 | assert.deepEqual(post.comment.attrs, comment.attrs); 28 | }); 29 | }); 30 | 31 | [ 32 | 'savedChildSavedParent', 33 | 'newChildSavedParent' 34 | ].forEach((state) => { 35 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 36 | let [ comment ] = this.helper[state](); 37 | 38 | comment.commentableId = null; 39 | 40 | assert.equal(comment.commentableId, null); 41 | assert.equal(comment.commentable, null); 42 | }); 43 | }); 44 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/10-one-to-one-polymorphic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-to-one Polymorphic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ comment, post ] = this.helper[state](); 13 | 14 | if (post) { 15 | post.destroy(); 16 | comment.reload(); 17 | } 18 | 19 | assert.equal(comment.commentableId, null); 20 | assert.deepEqual(comment.commentable, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/2-named/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ post, author ] = this.helper[state](); 16 | 17 | assert.deepEqual(post.author, author || null, 'the model reference is correct'); 18 | assert.equal(post.authorId, author ? author.id : null, 'the modelId reference is correct'); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/2-named/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ post ] = this.helper[state](); 16 | 17 | let ganon = post.createAuthor({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(post.author, ganon); 21 | assert.equal(post.authorId, ganon.id); 22 | assert.equal(this.helper.schema.posts.find(post.id).authorId, ganon.id, 'the child was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/2-named/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ post ] = this.helper[state](); 17 | 18 | let ganon = post.newAuthor({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(post.author, ganon); 22 | assert.equal(post.authorId, null); 23 | 24 | post.save(); 25 | 26 | assert.ok(ganon.id, 'saving the child persists the parent'); 27 | assert.equal(post.authorId, ganon.id, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/2-named/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ post ] = this.helper[state](); 16 | let savedAuthor = this.helper.savedParent(); 17 | 18 | post.authorId = savedAuthor.id; 19 | 20 | assert.equal(post.authorId, savedAuthor.id); 21 | assert.deepEqual(post.author, savedAuthor); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ post ] = this.helper[state](); 31 | 32 | post.authorId = null; 33 | 34 | assert.equal(post.authorId, null); 35 | assert.deepEqual(post.author, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/2-named/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ post ] = this.helper[state](); 16 | let savedAuthor = this.helper.savedParent(); 17 | 18 | post.author = savedAuthor; 19 | 20 | assert.equal(post.authorId, savedAuthor.id); 21 | assert.deepEqual(post.author, savedAuthor); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ post ] = this.helper[state](); 26 | let newAuthor = this.helper.newParent(); 27 | 28 | post.author = newAuthor; 29 | 30 | assert.equal(post.authorId, null); 31 | assert.deepEqual(post.author, newAuthor); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ post ] = this.helper[state](); 36 | 37 | post.author = null; 38 | 39 | assert.equal(post.authorId, null); 40 | assert.deepEqual(post.author, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/2-named/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ post, user ] = this.helper[state](); 13 | 14 | if (user) { 15 | user.destroy(); 16 | post.reload(); 17 | } 18 | 19 | assert.equal(post.authorId, null); 20 | assert.deepEqual(post.author, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/3-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, friend ] = this.helper[state](); 16 | 17 | // We use .attrs here because otherwise deepEqual goes on infinite recursive comparison 18 | if (friend) { 19 | assert.deepEqual(user.user.attrs, friend.attrs, 'the model reference is correct'); 20 | assert.equal(user.userId, friend.id, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(user.user, null, 'the model reference is correct'); 23 | assert.equal(user.userId, null, 'the modelId reference is correct'); 24 | } 25 | 26 | // If there's a friend in this state, make sure the inverse association is correct 27 | if (friend) { 28 | assert.deepEqual(friend.user.attrs, user.attrs, 'the inverse model reference is correct'); 29 | assert.equal(friend.userId, user.id, 'the inverse modelId reference is correct'); 30 | } 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/3-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user, originalUser ] = this.helper[state](); 16 | 17 | let ganon = user.createUser({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(user.user.attrs, ganon.attrs); 21 | assert.deepEqual(ganon.user.attrs, user.attrs, 'the inverse was set'); 22 | assert.equal(user.userId, ganon.id); 23 | assert.equal(ganon.userId, user.id, 'the inverse was set'); 24 | assert.equal(this.helper.schema.users.find(user.id).userId, ganon.id, 'the user was persisted'); 25 | 26 | if (originalUser) { 27 | originalUser.reload(); 28 | assert.equal(originalUser.userId, null, 'old inverses were cleared out'); 29 | } 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/3-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user, originalUser ] = this.helper[state](); 17 | 18 | let ganon = user.newUser({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(user.user, ganon); 22 | assert.equal(user.userId, null); 23 | assert.deepEqual(ganon.user, user, 'the inverse was set'); 24 | 25 | user.save(); 26 | 27 | assert.ok(ganon.id, 'saving the child persists the parent'); 28 | assert.equal(user.userId, ganon.id, 'the childs fk was updated'); 29 | 30 | if (originalUser) { 31 | originalUser.reload(); 32 | assert.equal(originalUser.userId, null, 'old inverses were cleared out'); 33 | } 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/3-reflexive/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Reflexive | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user, originalUser ] = this.helper[state](); 16 | let friend = this.helper.savedParent(); 17 | 18 | user.userId = friend.id; 19 | 20 | assert.equal(user.userId, friend.id); 21 | assert.deepEqual(user.user.attrs, friend.attrs); 22 | 23 | user.save(); 24 | if (originalUser) { 25 | originalUser.reload(); 26 | assert.equal(originalUser.userId, null, 'old inverses were cleared out'); 27 | } 28 | }); 29 | }); 30 | 31 | [ 32 | 'savedChildSavedParent', 33 | 'newChildSavedParent' 34 | ].forEach((state) => { 35 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 36 | let [ user, originalUser ] = this.helper[state](); 37 | 38 | user.userId = null; 39 | 40 | assert.equal(user.userId, null); 41 | assert.equal(user.user, null); 42 | 43 | user.save(); 44 | if (originalUser) { 45 | originalUser.reload(); 46 | assert.equal(originalUser.userId, null, 'old inverses were cleared out'); 47 | } 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/3-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ user, targetUser ] = this.helper[state](); 13 | 14 | if (targetUser) { 15 | targetUser.destroy(); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.userId, null); 20 | assert.deepEqual(user.user, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/4-named-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, friend ] = this.helper[state](); 16 | 17 | // We use .attrs here because otherwise deepEqual goes on infinite recursive comparison 18 | if (friend) { 19 | assert.deepEqual(user.bestFriend.attrs, friend.attrs, 'the model reference is correct'); 20 | assert.equal(user.bestFriendId, friend.id, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(user.bestFriend, null, 'the model reference is correct'); 23 | assert.equal(user.bestFriendId, null, 'the modelId reference is correct'); 24 | } 25 | 26 | // If there's a friend in this state, make sure the inverse association is correct 27 | if (friend) { 28 | assert.deepEqual(friend.bestFriend.attrs, user.attrs, 'the inverse model reference is correct'); 29 | assert.equal(friend.bestFriendId, user.id, 'the inverse modelId reference is correct'); 30 | } 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/4-named-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | 17 | let ganon = user.createBestFriend({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(user.bestFriend.attrs, ganon.attrs); 21 | assert.equal(user.bestFriendId, ganon.id); 22 | assert.equal(this.helper.schema.users.find(user.id).bestFriendId, ganon.id, 'the user was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/4-named-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | 18 | let ganon = user.newBestFriend({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(user.bestFriend, ganon); 22 | assert.equal(user.bestFriendId, null); 23 | 24 | user.save(); 25 | 26 | assert.ok(ganon.id, 'saving the child persists the parent'); 27 | assert.equal(user.bestFriendId, ganon.id, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/4-named-reflexive/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let friend = this.helper.savedParent(); 17 | 18 | user.bestFriendId = friend.id; 19 | 20 | assert.equal(user.bestFriendId, friend.id); 21 | assert.deepEqual(user.bestFriend.attrs, friend.attrs); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ user ] = this.helper[state](); 31 | 32 | user.bestFriendId = null; 33 | 34 | assert.equal(user.bestFriendId, null); 35 | assert.equal(user.bestFriend, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/4-named-reflexive/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let friend = this.helper.savedParent(); 17 | 18 | user.bestFriend = friend; 19 | 20 | assert.equal(user.bestFriendId, friend.id); 21 | assert.deepEqual(user.bestFriend.attrs, friend.attrs); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | let friend = this.helper.newParent(); 27 | 28 | user.bestFriend = friend; 29 | 30 | assert.equal(user.bestFriendId, null); 31 | assert.deepEqual(user.bestFriend.attrs, friend.attrs); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ user ] = this.helper[state](); 36 | 37 | user.bestFriend = null; 38 | 39 | assert.equal(user.bestFriendId, null); 40 | assert.deepEqual(user.bestFriend, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/4-named-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ user, bestFriend ] = this.helper[state](); 13 | 14 | if (bestFriend) { 15 | bestFriend.destroy(); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.bestFriendId, null); 20 | assert.deepEqual(user.bestFriend, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/5-named-reflexive-explicit-inverse/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive Explicit Inverse | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, friend ] = this.helper[state](); 16 | 17 | // We use .attrs here because otherwise deepEqual goes on infinite recursive comparison 18 | if (friend) { 19 | assert.deepEqual(user.bestFriend.attrs, friend.attrs, 'the model reference is correct'); 20 | assert.equal(user.bestFriendId, friend.id, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(user.bestFriend, null, 'the model reference is correct'); 23 | assert.equal(user.bestFriendId, null, 'the modelId reference is correct'); 24 | } 25 | 26 | // If there's a friend in this state, make sure the inverse association is correct 27 | if (friend) { 28 | assert.deepEqual(friend.bestFriend.attrs, user.attrs, 'the inverse model reference is correct'); 29 | assert.equal(friend.bestFriendId, user.id, 'the inverse modelId reference is correct'); 30 | } 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/5-named-reflexive-explicit-inverse/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive Explicit Inverse | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | 17 | let ganon = user.createBestFriend({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(user.bestFriend.attrs, ganon.attrs); 21 | assert.equal(user.bestFriendId, ganon.id); 22 | assert.equal(this.helper.schema.users.find(user.id).bestFriendId, ganon.id, 'the user was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/5-named-reflexive-explicit-inverse/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive Explicit Inverse | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | 18 | let ganon = user.newBestFriend({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(user.bestFriend, ganon); 22 | assert.equal(user.bestFriendId, null); 23 | 24 | user.save(); 25 | 26 | assert.ok(ganon.id, 'saving the child persists the parent'); 27 | assert.equal(user.bestFriendId, ganon.id, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/5-named-reflexive-explicit-inverse/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive Explicit Inverse | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let friend = this.helper.savedParent(); 17 | 18 | user.bestFriendId = friend.id; 19 | 20 | assert.equal(user.bestFriendId, friend.id); 21 | assert.deepEqual(user.bestFriend.attrs, friend.attrs); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ user ] = this.helper[state](); 31 | 32 | user.bestFriendId = null; 33 | 34 | assert.equal(user.bestFriendId, null); 35 | assert.equal(user.bestFriend, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/5-named-reflexive-explicit-inverse/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive Explicit Inverse | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let friend = this.helper.savedParent(); 17 | 18 | user.bestFriend = friend; 19 | 20 | assert.equal(user.bestFriendId, friend.id); 21 | assert.deepEqual(user.bestFriend.attrs, friend.attrs); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | let friend = this.helper.newParent(); 27 | 28 | user.bestFriend = friend; 29 | 30 | assert.equal(user.bestFriendId, null); 31 | assert.deepEqual(user.bestFriend.attrs, friend.attrs); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ user ] = this.helper[state](); 36 | 37 | user.bestFriend = null; 38 | 39 | assert.equal(user.bestFriendId, null); 40 | assert.deepEqual(user.bestFriend, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/5-named-reflexive-explicit-inverse/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named Reflexive Explicit Inverse | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ user, bestFriend ] = this.helper[state](); 13 | 14 | if (bestFriend) { 15 | bestFriend.destroy(); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.bestFriendId, null); 20 | assert.deepEqual(user.bestFriend, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/6-one-way-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-Way Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, parent ] = this.helper[state](); 16 | 17 | // We use .attrs here to avoid infinite recursion 18 | if (parent) { 19 | assert.deepEqual(user.user.attrs, parent.attrs, 'the model reference is correct'); 20 | assert.equal(user.userId, parent.id, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(user.user, null, 'the model reference is correct'); 23 | assert.equal(user.userId, null, 'the modelId reference is correct'); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/6-one-way-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-Way Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ child ] = this.helper[state](); 16 | 17 | let ganon = child.createUser({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(child.user.attrs, ganon.attrs); 21 | assert.equal(child.userId, ganon.id); 22 | assert.equal(this.helper.schema.users.find(child.id).userId, ganon.id, 'the child was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/6-one-way-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-Way Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ child ] = this.helper[state](); 17 | 18 | let ganon = child.newUser({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(child.user, ganon); 22 | assert.equal(child.userId, null); 23 | 24 | child.save(); 25 | 26 | assert.ok(ganon.id, 'saving the child persists the parent'); 27 | assert.equal(child.userId, ganon.id, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/6-one-way-reflexive/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-Way Reflexive | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ child ] = this.helper[state](); 16 | let savedParent = this.helper.savedParent(); 17 | 18 | child.userId = savedParent.id; 19 | 20 | assert.equal(child.userId, savedParent.id); 21 | assert.deepEqual(child.user.attrs, savedParent.attrs); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ child ] = this.helper[state](); 31 | 32 | child.userId = null; 33 | 34 | assert.equal(child.userId, null); 35 | assert.deepEqual(child.user, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/6-one-way-reflexive/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-Way Reflexive | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ child ] = this.helper[state](); 16 | let savedParent = this.helper.savedParent(); 17 | 18 | child.user = savedParent; 19 | 20 | assert.equal(child.userId, savedParent.id); 21 | assert.deepEqual(child.user.attrs, savedParent.attrs); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ child ] = this.helper[state](); 26 | let newParent = this.helper.newParent(); 27 | 28 | child.user = newParent; 29 | 30 | assert.equal(child.userId, null); 31 | assert.deepEqual(child.user, newParent); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ child ] = this.helper[state](); 36 | 37 | child.user = null; 38 | 39 | assert.equal(child.userId, null); 40 | assert.deepEqual(child.user, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/6-one-way-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-Way Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ user, targetUser ] = this.helper[state](); 13 | 14 | if (targetUser) { 15 | targetUser.destroy(); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.userId, null); 20 | assert.deepEqual(user.user, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/7-named-one-way-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named One-Way Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, parent ] = this.helper[state](); 16 | 17 | // We use .attrs here to avoid infinite recursion 18 | if (parent) { 19 | assert.deepEqual(user.parent.attrs, parent.attrs, 'the model reference is correct'); 20 | assert.equal(user.parentId, parent.id, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(user.parent, null, 'the model reference is correct'); 23 | assert.equal(user.parentId, null, 'the modelId reference is correct'); 24 | } 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/7-named-one-way-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named One-Way Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ child ] = this.helper[state](); 16 | 17 | let ganon = child.createParent({ name: 'Ganon' }); 18 | 19 | assert.ok(ganon.id, 'the parent was persisted'); 20 | assert.deepEqual(child.parent.attrs, ganon.attrs); 21 | assert.equal(child.parentId, ganon.id); 22 | assert.equal(this.helper.schema.users.find(child.id).parentId, ganon.id, 'the child was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/7-named-one-way-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named One-Way Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ child ] = this.helper[state](); 17 | 18 | let ganon = child.newParent({ name: 'Ganon' }); 19 | 20 | assert.ok(!ganon.id, 'the parent was not persisted'); 21 | assert.deepEqual(child.parent, ganon); 22 | assert.equal(child.parentId, null); 23 | 24 | child.save(); 25 | 26 | assert.ok(ganon.id, 'saving the child persists the parent'); 27 | assert.equal(child.parentId, ganon.id, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/7-named-one-way-reflexive/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named One-Way Reflexive | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ child ] = this.helper[state](); 16 | let savedParent = this.helper.savedParent(); 17 | 18 | child.parentId = savedParent.id; 19 | 20 | assert.equal(child.parentId, savedParent.id); 21 | assert.deepEqual(child.parent.attrs, savedParent.attrs); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ child ] = this.helper[state](); 31 | 32 | child.parentId = null; 33 | 34 | assert.equal(child.parentId, null); 35 | assert.deepEqual(child.parent, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/7-named-one-way-reflexive/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named One-Way Reflexive | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ child ] = this.helper[state](); 16 | let savedParent = this.helper.savedParent(); 17 | 18 | child.parent = savedParent; 19 | 20 | assert.equal(child.parentId, savedParent.id); 21 | assert.deepEqual(child.parent.attrs, savedParent.attrs); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ child ] = this.helper[state](); 26 | let newParent = this.helper.newParent(); 27 | 28 | child.parent = newParent; 29 | 30 | assert.equal(child.parentId, null); 31 | assert.deepEqual(child.parent, newParent); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ child ] = this.helper[state](); 36 | 37 | child.parent = null; 38 | 39 | assert.equal(child.parentId, null); 40 | assert.deepEqual(child.parent, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/7-named-one-way-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Named One-Way Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ user, parent ] = this.helper[state](); 13 | 14 | if (parent) { 15 | parent.destroy(); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.parentId, null); 20 | assert.deepEqual(user.parent, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/8-one-to-one/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One To One | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, profile ] = this.helper[state](); 16 | 17 | // We use .attrs here because otherwise deepEqual goes on infinite recursive comparison 18 | if (profile) { 19 | assert.deepEqual(user.profile.attrs, profile.attrs, 'the model reference is correct'); 20 | assert.equal(user.profileId, profile.id, 'the modelId reference is correct'); 21 | } else { 22 | assert.deepEqual(user.profile, null, 'the model reference is correct'); 23 | assert.equal(user.profileId, null, 'the modelId reference is correct'); 24 | } 25 | 26 | // If there's a profile in this state, make sure the inverse association is correct 27 | if (profile) { 28 | assert.deepEqual(profile.user.attrs, user.attrs, 'the inverse model reference is correct'); 29 | assert.equal(profile.userId, user.id, 'the inverse modelId reference is correct'); 30 | } 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/8-one-to-one/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One To One | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | 17 | let profile = user.createProfile({ age: 300 }); 18 | 19 | assert.ok(profile.id, 'the parent was persisted'); 20 | assert.deepEqual(user.profile.attrs, profile.attrs); 21 | assert.deepEqual(profile.user.attrs, user.attrs, 'the inverse was set'); 22 | assert.equal(user.profileId, profile.id); 23 | assert.equal(this.helper.schema.users.find(user.id).profileId, profile.id, 'the user was persisted'); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/8-one-to-one/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One To One | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | 18 | let profile = user.newProfile({ age: 300 }); 19 | 20 | assert.ok(!profile.id, 'the parent was not persisted'); 21 | assert.deepEqual(user.profile, profile); 22 | assert.equal(user.profileId, null); 23 | assert.deepEqual(profile.user, user, 'the inverse was set'); 24 | assert.equal(profile.userId, user.id); 25 | 26 | user.save(); 27 | 28 | assert.ok(profile.id, 'saving the child persists the parent'); 29 | assert.equal(user.profileId, profile.id, 'the childs fk was updated'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/8-one-to-one/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One To One | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let profile = this.helper.savedParent(); 17 | 18 | user.profileId = profile.id; 19 | 20 | assert.equal(user.profileId, profile.id); 21 | assert.deepEqual(user.profile.attrs, profile.attrs); 22 | 23 | user.save(); 24 | profile.reload(); 25 | 26 | assert.equal(profile.userId, user.id, 'the inverse was set'); 27 | assert.deepEqual(profile.user.attrs, user.attrs); 28 | }); 29 | }); 30 | 31 | // [ 32 | // 'savedChildSavedParent', 33 | // 'newChildSavedParent' 34 | // ].forEach((state) => { 35 | // 36 | // test(`a ${state} can clear its association via a null parentId`, function(assert) { 37 | // let [ user ] = this.helper[state](); 38 | // 39 | // user.userId = null; 40 | // 41 | // assert.equal(user.userId, null); 42 | // assert.equal(user.user, null); 43 | // }); 44 | // 45 | // }); 46 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/8-one-to-one/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One To One | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let profile = this.helper.savedParent(); 17 | 18 | user.profile = profile; 19 | 20 | assert.equal(user.profileId, profile.id); 21 | assert.deepEqual(user.profile.attrs, profile.attrs); 22 | assert.equal(profile.userId, user.id, 'the inverse was set'); 23 | assert.deepEqual(profile.user.attrs, user.attrs); 24 | }); 25 | 26 | test(`a ${state} can update its association to a new parent`, function(assert) { 27 | let [ user ] = this.helper[state](); 28 | let profile = this.helper.newParent(); 29 | 30 | user.profile = profile; 31 | 32 | assert.equal(user.profileId, null); 33 | assert.deepEqual(user.profile.attrs, profile.attrs); 34 | 35 | assert.equal(profile.userId, user.id, 'the inverse was set'); 36 | assert.deepEqual(profile.user.attrs, user.attrs); 37 | }); 38 | 39 | test(`a ${state} can update its association to a null parent`, function(assert) { 40 | let [ user ] = this.helper[state](); 41 | 42 | user.profile = null; 43 | 44 | assert.equal(user.profileId, null); 45 | assert.deepEqual(user.profile, null); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/8-one-to-one/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One To One | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ user, profile ] = this.helper[state](); 13 | 14 | if (profile) { 15 | profile.destroy(); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.profileId, null); 20 | assert.deepEqual(user.profile, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/9-one-way-polymorphic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-way Polymorphic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ comment, post ] = this.helper[state](); 16 | 17 | assert.deepEqual(comment.commentable, post || null, 'the model reference is correct'); 18 | assert.deepEqual(comment.commentableId, post ? { id: post.id, type: 'post' } : null, 'the modelId reference is correct'); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/9-one-way-polymorphic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-way Polymorphic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a belongs-to association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ comment ] = this.helper[state](); 16 | 17 | let post = comment.createCommentable('post', { title: 'Lorem ipsum' }); 18 | 19 | assert.ok(post.id, 'the parent was persisted'); 20 | assert.deepEqual(comment.commentable.attrs, post.attrs); 21 | assert.deepEqual(comment.commentableId, { id: post.id, type: 'post' }); 22 | assert.ok(this.helper.db.posts.find(post.id), 'the child was persisted'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/9-one-way-polymorphic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-way Polymorphic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ comment ] = this.helper[state](); 17 | 18 | let post = comment.newCommentable('post', { title: 'Lorem ipsum' }); 19 | 20 | assert.ok(!post.id, 'the parent was not persisted'); 21 | assert.deepEqual(comment.commentable, post); 22 | assert.deepEqual(comment.commentableId, { id: undefined, type: 'post' }); 23 | 24 | comment.save(); 25 | 26 | assert.ok(post.id, 'saving the child persists the parent'); 27 | assert.deepEqual(comment.commentableId, { id: post.id, type: 'post' }, 'the childs fk was updated'); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/9-one-way-polymorphic/association-set-id-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-way Polymorphic | association #setId', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ comment ] = this.helper[state](); 16 | let savedPost = this.helper.savedParent(); 17 | 18 | comment.commentableId = { id: savedPost.id, type: 'post' }; 19 | 20 | assert.deepEqual(comment.commentableId, { id: savedPost.id, type: 'post' }); 21 | assert.deepEqual(comment.commentable.attrs, savedPost.attrs); 22 | }); 23 | }); 24 | 25 | [ 26 | 'savedChildSavedParent', 27 | 'newChildSavedParent' 28 | ].forEach((state) => { 29 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 30 | let [ comment ] = this.helper[state](); 31 | 32 | comment.commentableId = null; 33 | 34 | assert.equal(comment.commentableId, null); 35 | assert.deepEqual(comment.commentable, null); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/9-one-way-polymorphic/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-way Polymorphic | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent`, function(assert) { 15 | let [ comment ] = this.helper[state](); 16 | let savedPost = this.helper.savedParent(); 17 | 18 | comment.commentable = savedPost; 19 | 20 | assert.deepEqual(comment.commentableId, { id: savedPost.id, type: 'post' }); 21 | assert.deepEqual(comment.commentable, savedPost); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ comment ] = this.helper[state](); 26 | let newPost = this.helper.newParent(); 27 | 28 | comment.commentable = newPost; 29 | 30 | assert.deepEqual(comment.commentableId, { id: undefined, type: 'post' }); 31 | assert.deepEqual(comment.commentable, newPost); 32 | }); 33 | 34 | test(`a ${state} can update its association to a null parent`, function(assert) { 35 | let [ comment ] = this.helper[state](); 36 | 37 | comment.commentable = null; 38 | 39 | assert.equal(comment.commentableId, null); 40 | assert.deepEqual(comment.commentable, null); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/9-one-way-polymorphic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | One-way Polymorphic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting the parent updates the child's foreign key for a ${state}`, function(assert) { 12 | let [ comment, post ] = this.helper[state](); 13 | 14 | if (post) { 15 | post.destroy(); 16 | comment.reload(); 17 | } 18 | 19 | assert.equal(comment.commentableId, null); 20 | assert.equal(comment.post, null); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/misc-test.js: -------------------------------------------------------------------------------- 1 | // import { Schema, Db, Model, belongsTo } from '@bigtest/mirage'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Misc'); 5 | 6 | test('an ambiguous schema throws an error', function(assert) { 7 | assert.ok(true); 8 | // assert.throws(function() { 9 | // new Schema(new Db(), { 10 | // user: Model.extend({ 11 | // foo: belongsTo('user'), 12 | // bar: belongsTo('user') 13 | // }) 14 | // }); 15 | // }, /You defined the 'foo' relationship on user, but multiple possible inverse relationships of type user exist. Please refer to the models documentation to learn how to explicitly specify inverses./); 16 | }); 17 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/notes.md: -------------------------------------------------------------------------------- 1 | A belongsTo relationship has the following possible schemas. For each schema, the child model can be in six states with respect to its parent: 2 | 3 | - the child can be saved and the parent can be one of: undefined, new or saved 4 | 5 | - the child can be new and the parent can be one of: undefined, new or saved 6 | 7 | This is how the tests in this directory are organized. 8 | 9 | # belongsTo 10 | Given Post, Author models 11 | For the Post model 12 | 13 | ## basic 14 | author: belongsTo() 15 | 16 | ## named 17 | writer: belongsTo('author') 18 | 19 | ## reflexive, one-way 20 | post: belongsTo() 21 | 22 | ## named reflexive 23 | childPost: belongsTo('post') 24 | 25 | ## inverse (implicit) 26 | author: belongsTo() 27 | 28 | (author) 29 | posts: hasMany() 30 | 31 | ## inverse (explicit) 32 | author: belongsTo('author', { inverse: 'redPosts' }) 33 | 34 | (author) 35 | posts: hasMany('post', { inverse: 'author' }) 36 | drafts: hasMany('post') 37 | 38 | ## multiple (conflict) 39 | primaryAuthor: belongsTo('author') 40 | secondaryAuthor: belongsTo('author') 41 | 42 | (author) 43 | posts: hasMany() 44 | -------------------------------------------------------------------------------- /tests/integration/orm/belongs-to/regressions/issue-1112-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db, belongsTo, hasMany } from '@bigtest/mirage'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Belongs To | Regressions | Issue 1112'); 5 | 6 | test(`deleting a record with a polymorphic belongsTo doesn't interfere with other dependents`, function(assert) { 7 | let schema = new Schema(new Db(), { 8 | comment: Model.extend({ 9 | commentable: belongsTo({ polymorphic: true }), 10 | user: belongsTo('user') 11 | }), 12 | 13 | post: Model, 14 | 15 | user: Model.extend({ 16 | comments: hasMany('comment') 17 | }) 18 | }); 19 | 20 | let user = schema.users.create(); 21 | let post = schema.posts.create(); 22 | let comment = schema.comments.create({ commentable: post, user }); 23 | 24 | comment.destroy(); 25 | 26 | assert.deepEqual(user.reload().commentIds, []); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/integration/orm/collection-inflector-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db } from '@bigtest/mirage'; 2 | import { inflections } from 'inflected'; 3 | 4 | import {module, test} from 'qunit'; 5 | 6 | var db, schema, HeadOfState; 7 | module('Integration | ORM | inflector-collectionName integration', { 8 | beforeEach() { 9 | inflections('en').irregular('head-of-state', 'heads-of-state'); 10 | 11 | HeadOfState = Model.extend(); 12 | db = new Db({}); 13 | schema = new Schema(db); 14 | schema.registerModel('headOfState', HeadOfState); 15 | } 16 | }); 17 | 18 | test(' [regression] collection creation respects irregular plural rules', function(assert) { 19 | assert.equal(schema.db._collections.length, 1); 20 | assert.equal(schema.db._collections[0].name, 'headsOfState'); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/integration/orm/create-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Db, Model } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | var db, schema, User; 5 | module('Integration | ORM | create', { 6 | beforeEach() { 7 | User = Model.extend(); 8 | db = new Db(); 9 | schema = new Schema(db, { 10 | user: User 11 | }); 12 | } 13 | }); 14 | 15 | test('it cannot make new models that havent been registered', function(assert) { 16 | assert.throws(function() { 17 | schema.authors.new({ name: 'Link' }); 18 | }); 19 | }); 20 | 21 | test('it cannot create models that havent been registered', function(assert) { 22 | assert.throws(function() { 23 | schema.authors.create({ name: 'Link' }); 24 | }); 25 | }); 26 | 27 | test('it can make new models and then save them', function(assert) { 28 | let user = schema.users.new({ name: 'Link' }); 29 | 30 | assert.ok(user instanceof User); 31 | assert.deepEqual(user.attrs, { name: 'Link' }); 32 | assert.deepEqual(db.users, []); 33 | 34 | user.save(); 35 | 36 | assert.ok(user.id, 'user has an id getter'); 37 | assert.deepEqual(user.attrs, { id: '1', name: 'Link' }); 38 | assert.deepEqual(db.users, [{ id: '1', name: 'Link' }]); 39 | }); 40 | 41 | test('it can create new models, saved directly to the db', function(assert) { 42 | let user = schema.users.create({ name: 'Link' }); 43 | 44 | assert.ok(user instanceof Model); 45 | assert.ok(user instanceof User); 46 | assert.deepEqual(user.attrs, { id: '1', name: 'Link' }); 47 | assert.deepEqual(db.users, [{ id: '1', name: 'Link' }]); 48 | }); 49 | -------------------------------------------------------------------------------- /tests/integration/orm/destroy-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Db, Model } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | let db; 5 | module('Integration | ORM | destroy', { 6 | beforeEach() { 7 | db = new Db({ 8 | users: [ 9 | { id: 1, name: 'Link', evil: false }, 10 | { id: 2, name: 'Link', location: 'Hyrule', evil: false }, 11 | { id: 3, name: 'Zelda', location: 'Hyrule', evil: false } 12 | ] 13 | }); 14 | 15 | this.schema = new Schema(db, { 16 | user: Model 17 | }); 18 | } 19 | }); 20 | 21 | test('destroying a model removes the associated record from the db', function(assert) { 22 | assert.deepEqual(db.users.length, 3); 23 | 24 | let link = this.schema.users.find(1); 25 | link.destroy(); 26 | 27 | assert.deepEqual(db.users.find(1), null); 28 | assert.deepEqual(db.users.length, 2); 29 | }); 30 | 31 | test('destroying a collection removes the associated records from the db', function(assert) { 32 | assert.deepEqual(db.users.length, 3); 33 | 34 | let users = this.schema.users.all(); 35 | users.destroy(); 36 | 37 | assert.deepEqual(db.users, []); 38 | }); 39 | -------------------------------------------------------------------------------- /tests/integration/orm/find-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db, Collection } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | var schema; 5 | var User = Model.extend(); 6 | module('Integration | ORM | #find', { 7 | beforeEach() { 8 | let db = new Db({ users: [ 9 | { id: 1, name: 'Link' }, 10 | { id: 2, name: 'Zelda' } 11 | ] }); 12 | 13 | schema = new Schema(db, { 14 | user: User 15 | }); 16 | } 17 | }); 18 | 19 | test('it can find a model by id', function(assert) { 20 | let zelda = schema.users.find(2); 21 | 22 | assert.ok(zelda instanceof User); 23 | assert.deepEqual(zelda.attrs, { id: '2', name: 'Zelda' }); 24 | }); 25 | 26 | test('it returns null if no model is found for an id', function(assert) { 27 | let user = schema.users.find(4); 28 | 29 | assert.equal(user, null); 30 | }); 31 | 32 | test('it can find multiple models by ids', function(assert) { 33 | let users = schema.users.find([1, 2]); 34 | 35 | assert.ok(users instanceof Collection, 'it returns a collection'); 36 | assert.ok(users.models[0] instanceof User); 37 | assert.equal(users.models.length, 2); 38 | assert.deepEqual(users.models[1].attrs, { id: '2', name: 'Zelda' }); 39 | }); 40 | 41 | test('it errors if incorrect number of models are found for an array of ids', function(assert) { 42 | assert.throws(function() { 43 | schema.users.find([1, 6]); 44 | }, /Couldn't find all users/); 45 | }); 46 | -------------------------------------------------------------------------------- /tests/integration/orm/first-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Db, Model } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | let schema; 5 | let User = Model.extend(); 6 | module('Integration | ORM | #first', { 7 | beforeEach() { 8 | let db = new Db(); 9 | db.createCollection('users'); 10 | db.users.insert([{ id: 1, name: 'Link' }, { id: 2, name: 'Zelda' }]); 11 | schema = new Schema(db); 12 | 13 | schema.registerModel('user', User); 14 | } 15 | }); 16 | 17 | test('it can find the first model', function(assert) { 18 | let user = schema.users.first(); 19 | 20 | assert.ok(user instanceof User); 21 | assert.deepEqual(user.attrs, { id: '1', name: 'Link' }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/1-basic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Basic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, posts ] = this.helper[state](); 16 | 17 | assert.equal(user.posts.models.length, posts.length, 'the parent has the correct number of children'); 18 | assert.equal(user.postIds.length, posts.length, 'the parent has the correct number of children ids'); 19 | 20 | posts.forEach((post, i) => { 21 | assert.deepEqual(user.posts.models[i], posts[i], 'each child is in parent.children array'); 22 | 23 | if (post.isSaved()) { 24 | assert.ok(user.postIds.indexOf(post.id) > -1, 'each saved child id is in parent.childrenIds array'); 25 | } 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/1-basic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Basic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let initialCount = user.posts.models.length; 17 | 18 | let post = user.createPost({ title: 'Lorem ipsum' }); 19 | 20 | assert.ok(post.id, 'the child was persisted'); 21 | assert.equal(user.posts.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(user.posts.includes(post), 'the model was added to user.posts'); 23 | assert.ok(user.postIds.indexOf(post.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(user.attrs.postIds.indexOf(post.id) > -1, 'fks were persisted'); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/1-basic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Basic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | let initialCount = user.posts.models.length; 18 | 19 | let post = user.newPost({ title: 'Lorem ipsum' }); 20 | 21 | assert.ok(!post.id, 'the child was not persisted'); 22 | assert.equal(user.posts.models.length, initialCount + 1); 23 | 24 | post.save(); 25 | 26 | assert.deepEqual(post.attrs, { id: post.id, title: 'Lorem ipsum' }, 'the child was persisted'); 27 | assert.equal(user.posts.models.length, initialCount + 1, 'the collection size was increased'); 28 | assert.deepEqual(user.posts.models.filter((a) => a.id === post.id)[0], post, 'the model was added to user.posts'); 29 | assert.ok(user.postIds.indexOf(post.id) > -1, 'the id was added to the fks array'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/1-basic/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Basic | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.postIds = [ savedPost.id ]; 19 | 20 | assert.deepEqual(user.posts.models[0].attrs, savedPost.attrs); 21 | assert.deepEqual(user.postIds, [ savedPost.id ]); 22 | }); 23 | 24 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | 27 | user.postIds = null; 28 | 29 | assert.deepEqual(user.posts.models, []); 30 | assert.deepEqual(user.postIds, []); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/1-basic/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Basic | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a list of saved children`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.posts = [ savedPost ]; 19 | 20 | assert.ok(user.posts.models.indexOf(savedPost) > -1); 21 | assert.ok(user.postIds.indexOf(savedPost.id) > -1); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | let newPost = this.helper.newChild(); 27 | 28 | user.posts = [ newPost ]; 29 | 30 | assert.deepEqual(user.postIds, [ undefined ]); 31 | assert.deepEqual(user.posts.models[0], newPost); 32 | }); 33 | 34 | test(`a ${state} can clear its association via an empty list`, function(assert) { 35 | let [ user ] = this.helper[state](); 36 | 37 | user.posts = [ ]; 38 | 39 | assert.deepEqual(user.postIds, [ ]); 40 | assert.equal(user.posts.models.length, 0); 41 | }); 42 | 43 | test(`a ${state} can clear its association via an empty list`, function(assert) { 44 | let [ user ] = this.helper[state](); 45 | 46 | user.posts = null; 47 | 48 | assert.deepEqual(user.postIds, [ ]); 49 | assert.equal(user.posts.models.length, 0); 50 | }); 51 | }); 52 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/1-basic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Basic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ user, posts ] = this.helper[state](); 13 | 14 | if (posts && posts.length) { 15 | posts.forEach(p => p.destroy()); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.posts.length, 0); 20 | assert.equal(user.postIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/10-many-to-many-polymorphic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many-to-many Polymorphic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, posts ] = this.helper[state](); 16 | 17 | assert.equal(user.commentables.models.length, posts.length, 'the parent has the correct number of children'); 18 | assert.equal(user.commentableIds.length, posts.length, 'the parent has the correct number of children ids'); 19 | 20 | posts.forEach((post, i) => { 21 | assert.ok(user.commentables.includes(post), 'each child is in parent.children array'); 22 | 23 | if (post.isSaved()) { 24 | assert.deepEqual( 25 | user.commentableIds[i], 26 | { type: 'post', id: post.id }, 27 | 'each saved child id is in parent.childrenIds array' 28 | ); 29 | } 30 | 31 | // Check the inverse 32 | assert.ok(post.users.includes(user)); 33 | }); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/10-many-to-many-polymorphic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many-to-many Polymorphic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let initialCount = user.commentables.models.length; 17 | 18 | let post = user.createCommentable('post', { title: 'Lorem ipsum' }); 19 | 20 | assert.ok(post.id, 'the child was persisted'); 21 | assert.equal(user.commentables.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(user.commentables.includes(post), 'the model was added to the association'); 23 | assert.ok(user.commentableIds.find(obj => { 24 | return (obj.id === post.id && obj.type === 'post'); 25 | }), 'the id was added to the fks array'); 26 | assert.ok(user.attrs.commentableIds.find(obj => { 27 | return (obj.id === post.id && obj.type === 'post'); 28 | }), 'fks were persisted'); 29 | assert.ok(post.users.includes(user), 'the inverse was set'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/10-many-to-many-polymorphic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many-to-many Polymorphic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | let initialCount = user.commentables.models.length; 18 | 19 | let post = user.newCommentable('post', { title: 'Lorem ipsum' }); 20 | 21 | assert.ok(!post.id, 'the child was not persisted'); 22 | assert.equal(user.commentables.models.length, initialCount + 1); 23 | assert.equal(post.users.models.length, 1, 'the inverse was set'); 24 | 25 | post.save(); 26 | 27 | assert.deepEqual(post.attrs, { id: post.id, title: 'Lorem ipsum', userIds: [ user.id ] }, 'the child was persisted'); 28 | assert.equal(user.commentables.models.length, initialCount + 1, 'the collection size was increased'); 29 | assert.ok(user.commentables.includes(post), 'the model was added to user.commentables'); 30 | assert.ok(user.commentableIds.find(obj => { 31 | return (obj.id === post.id && obj.type === 'post'); 32 | }), 'the id was added to the fks array'); 33 | assert.ok(post.users.includes(user), 'the inverse was set'); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/10-many-to-many-polymorphic/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many-to-many Polymorphic | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user, originalPosts ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.commentableIds = [ { type: 'post', id: savedPost.id } ]; 19 | 20 | assert.ok(user.commentables.includes(savedPost)); 21 | assert.ok(user.commentableIds.find(({ id, type }) => ((id === savedPost.id && type === 'post')))); 22 | 23 | user.save(); 24 | savedPost.reload(); 25 | 26 | assert.ok(savedPost.users.includes(user), 'the inverse was set'); 27 | originalPosts.forEach(post => { 28 | if (post.isSaved()) { 29 | post.reload(); 30 | assert.notOk(post.users.includes(user), 'old inverses were cleared'); 31 | } 32 | }); 33 | }); 34 | 35 | test(`a ${state} can clear its association via a null ids`, function(assert) { 36 | let [ user, originalPosts ] = this.helper[state](); 37 | 38 | user.commentableIds = null; 39 | 40 | assert.deepEqual(user.commentables.models, []); 41 | assert.deepEqual(user.commentableIds, []); 42 | 43 | user.save(); 44 | 45 | originalPosts.forEach(post => { 46 | post.reload(); 47 | assert.notOk(post.users.includes(user), 'old inverses were cleared'); 48 | }); 49 | }); 50 | }); 51 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/10-many-to-many-polymorphic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many-to-many Polymorphic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ user, posts ] = this.helper[state](); 13 | 14 | if (posts && posts.length) { 15 | posts.forEach(p => p.destroy()); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.commentables.length, 0); 20 | assert.equal(user.commentableIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/2-named/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, posts ] = this.helper[state](); 16 | 17 | assert.equal(user.blogPosts.models.length, posts.length, 'the parent has the correct number of children'); 18 | assert.equal(user.blogPostIds.length, posts.length, 'the parent has the correct number of children ids'); 19 | 20 | posts.forEach((post, i) => { 21 | assert.deepEqual(user.blogPosts.models[i], posts[i], 'each child is in parent.children array'); 22 | 23 | if (post.isSaved()) { 24 | assert.ok(user.blogPostIds.indexOf(post.id) > -1, 'each saved child id is in parent.childrenIds array'); 25 | } 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/2-named/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let initialCount = user.blogPosts.models.length; 17 | 18 | let post = user.createBlogPost({ title: 'Lorem ipsum' }); 19 | 20 | assert.ok(post.id, 'the child was persisted'); 21 | assert.equal(user.blogPosts.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(user.blogPosts.includes(post), 'the model was added to user.blogPosts'); 23 | assert.ok(user.blogPostIds.indexOf(post.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(user.attrs.blogPostIds.indexOf(post.id) > -1, 'fks were persisted'); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/2-named/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | let initialCount = user.blogPosts.models.length; 18 | 19 | let post = user.newBlogPost({ title: 'Lorem ipsum' }); 20 | 21 | assert.ok(!post.id, 'the child was not persisted'); 22 | assert.equal(user.blogPosts.models.length, initialCount + 1); 23 | 24 | post.save(); 25 | 26 | assert.deepEqual(post.attrs, { id: post.id, title: 'Lorem ipsum' }, 'the child was persisted'); 27 | assert.equal(user.blogPosts.models.length, initialCount + 1, 'the collection size was increased'); 28 | assert.deepEqual(user.blogPosts.models.filter((a) => a.id === post.id)[0], post, 'the model was added to user.blogPosts'); 29 | assert.ok(user.blogPostIds.indexOf(post.id) > -1, 'the id was added to the fks array'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/2-named/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.blogPostIds = [ savedPost.id ]; 19 | 20 | assert.deepEqual(user.blogPosts.models[0].attrs, savedPost.attrs); 21 | assert.deepEqual(user.blogPostIds, [ savedPost.id ]); 22 | }); 23 | 24 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | 27 | user.blogPostIds = null; 28 | 29 | assert.deepEqual(user.blogPosts.models, []); 30 | assert.deepEqual(user.blogPostIds, []); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/2-named/association-set-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named | association #set', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parent, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a list of saved children`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.blogPosts = [ savedPost ]; 19 | 20 | assert.ok(user.blogPosts.models.indexOf(savedPost) > -1); 21 | assert.ok(user.blogPostIds.indexOf(savedPost.id) > -1); 22 | }); 23 | 24 | test(`a ${state} can update its association to a new parent`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | let newPost = this.helper.newChild(); 27 | 28 | user.blogPosts = [ newPost ]; 29 | 30 | assert.deepEqual(user.blogPostIds, [ undefined ]); 31 | assert.deepEqual(user.blogPosts.models[0], newPost); 32 | }); 33 | 34 | test(`a ${state} can clear its association via an empty list`, function(assert) { 35 | let [ user ] = this.helper[state](); 36 | 37 | user.blogPosts = [ ]; 38 | 39 | assert.deepEqual(user.blogPostIds, [ ]); 40 | assert.equal(user.blogPosts.models.length, 0); 41 | }); 42 | 43 | test(`a ${state} can clear its association via an empty list`, function(assert) { 44 | let [ user ] = this.helper[state](); 45 | 46 | user.blogPosts = null; 47 | 48 | assert.deepEqual(user.blogPostIds, [ ]); 49 | assert.equal(user.blogPosts.models.length, 0); 50 | }); 51 | }); 52 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/2-named/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ user, blogPosts ] = this.helper[state](); 13 | 14 | if (blogPosts && blogPosts.length) { 15 | blogPosts.forEach(p => p.destroy()); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.blogPosts.length, 0); 20 | assert.equal(user.blogPostIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/3-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`the references of a ${state} are correct`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | assert.equal(tag.tags.models.length, tags.length, 'the parent has the correct number of children'); 15 | assert.equal(tag.tagIds.length, tags.length, 'the parent has the correct number of children ids'); 16 | 17 | tags.forEach((t, i) => { 18 | assert.deepEqual(tag.tags.models[i], t, 'each child is in parent.children array'); 19 | 20 | if (t.isSaved()) { 21 | assert.ok(tag.tagIds.indexOf(t.id) > -1, 'each saved child id is in parent.childrenIds array'); 22 | } 23 | 24 | // Check the inverse 25 | assert.ok(t.tags.includes(tag)); 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/3-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated child`, function(assert) { 15 | let [ tag ] = this.helper[state](); 16 | let initialCount = tag.tags.models.length; 17 | 18 | let orangeTag = tag.createTag({ name: 'Orange' }); 19 | let blueTag = tag.createTag({ name: 'Blue' }); 20 | 21 | assert.ok(orangeTag.id, 'the child was persisted'); 22 | assert.ok(blueTag.id, 'the child was persisted'); 23 | assert.equal(tag.tags.models.length, initialCount + 2, 'the collection size was increased'); 24 | assert.ok(tag.tags.includes(orangeTag), 'the model was added to tag.tags'); 25 | assert.ok(tag.tags.includes(blueTag), 'the model was added to tag.tags'); 26 | assert.ok(tag.tagIds.indexOf(orangeTag.id) > -1, 'the id was added to the fks array'); 27 | assert.ok(tag.tagIds.indexOf(blueTag.id) > -1, 'the id was added to the fks array'); 28 | assert.ok(tag.attrs.tagIds.indexOf(orangeTag.id) > -1, 'fks were persisted'); 29 | assert.ok(tag.attrs.tagIds.indexOf(blueTag.id) > -1, 'fks were persisted'); 30 | 31 | // Check the inverse 32 | assert.equal(orangeTag.tags.models.length, 1); 33 | assert.ok(orangeTag.tags.includes(tag), 'the inverse was set'); 34 | assert.equal(blueTag.tags.models.length, 1); 35 | assert.ok(blueTag.tags.includes(tag), 'the inverse was set'); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/3-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated child`, function(assert) { 16 | let [ tag ] = this.helper[state](); 17 | let initialCount = tag.tags.models.length; 18 | 19 | let blueTag = tag.newTag({ name: 'Blue' }); 20 | 21 | assert.ok(!blueTag.id, 'the child was not persisted'); 22 | assert.equal(tag.tags.models.length, initialCount + 1); 23 | assert.equal(blueTag.tags.models.length, 1, 'the inverse was set'); 24 | 25 | blueTag.save(); 26 | tag.reload(); 27 | 28 | assert.deepEqual(blueTag.attrs, { id: blueTag.id, name: 'Blue', tagIds: [ tag.id ] }, 'the child was persisted'); 29 | assert.equal(tag.tags.models.length, initialCount + 1, 'the collection size was increased'); 30 | assert.ok(tag.tags.includes(blueTag), 'the model was added to tag.tags'); 31 | assert.ok(tag.tagIds.indexOf(blueTag.id) > -1, 'the id was added to the fks array'); 32 | assert.ok(blueTag.tags.includes(tag), 'the inverse was set'); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/3-reflexive/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Reflexive | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`a ${state} can update its association to include a saved child via childIds`, function(assert) { 12 | let [ tag, originalTags ] = this.helper[state](); 13 | let savedTag = this.helper.savedChild(); 14 | 15 | tag.tagIds = [ savedTag.id ]; 16 | 17 | assert.deepEqual(tag.tags.models[0].attrs, savedTag.attrs); 18 | assert.deepEqual(tag.tagIds, [ savedTag.id ]); 19 | 20 | tag.save(); 21 | savedTag.reload(); 22 | 23 | assert.deepEqual(savedTag.tags.models[0].attrs, tag.attrs, 'the inverse was set'); 24 | originalTags.forEach(originalTag => { 25 | if (originalTag.isSaved()) { 26 | originalTag.reload(); 27 | assert.notOk(originalTag.tags.includes(tag), 'old inverses were cleared'); 28 | } 29 | }); 30 | }); 31 | 32 | test(`a ${state} can clear its association via a null childIds`, function(assert) { 33 | let [ tag, originalTags ] = this.helper[state](); 34 | 35 | tag.tagIds = null; 36 | 37 | assert.deepEqual(tag.tags.models, []); 38 | assert.deepEqual(tag.tagIds, []); 39 | 40 | tag.save(); 41 | originalTags.forEach(originalTag => { 42 | originalTag.reload(); 43 | assert.notOk(originalTag.tags.includes(tag), 'old inverses were cleared'); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/3-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | if (tags && tags.length) { 15 | tags.forEach(t => t.destroy()); 16 | tag.reload(); 17 | } 18 | 19 | assert.equal(tag.tags.length, 0); 20 | assert.equal(tag.tagIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/4-named-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`the references of a ${state} are correct`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | assert.equal(tag.labels.models.length, tags.length, 'the parent has the correct number of children'); 15 | assert.equal(tag.labelIds.length, tags.length, 'the parent has the correct number of children ids'); 16 | 17 | tags.forEach((t, i) => { 18 | assert.deepEqual(tag.labels.models[i], t, 'each child is in parent.children array'); 19 | 20 | if (t.isSaved()) { 21 | assert.ok(tag.labelIds.indexOf(t.id) > -1, 'each saved child id is in parent.childrenIds array'); 22 | } 23 | 24 | // Check the inverse 25 | assert.ok(t.labels.includes(tag)); 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/4-named-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated child`, function(assert) { 15 | let [ tag ] = this.helper[state](); 16 | let initialCount = tag.labels.models.length; 17 | 18 | let orangeTag = tag.createLabel({ name: 'Orange' }); 19 | 20 | assert.ok(orangeTag.id, 'the child was persisted'); 21 | assert.equal(tag.labels.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(tag.labels.includes(orangeTag), 'the model was added to tag.labels'); 23 | assert.ok(tag.labelIds.indexOf(orangeTag.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(tag.attrs.labelIds.indexOf(orangeTag.id) > -1, 'fks were persisted'); 25 | assert.ok(orangeTag.labels.includes(tag), 'the inverse was set'); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/4-named-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated child`, function(assert) { 16 | let [ tag ] = this.helper[state](); 17 | let initialCount = tag.labels.models.length; 18 | 19 | let blueTag = tag.newLabel({ name: 'Blue' }); 20 | 21 | assert.ok(!blueTag.id, 'the child was not persisted'); 22 | assert.equal(tag.labels.models.length, initialCount + 1); 23 | assert.equal(blueTag.labels.models.length, 1, 'the inverse was set'); 24 | 25 | blueTag.save(); 26 | 27 | assert.deepEqual(blueTag.attrs, { id: blueTag.id, name: 'Blue', labelIds: [ tag.id ] }, 'the child was persisted'); 28 | assert.equal(tag.labels.models.length, initialCount + 1, 'the collection size was increased'); 29 | assert.ok(tag.labels.includes(blueTag), 'the model was added to tag.labels'); 30 | assert.ok(tag.labelIds.indexOf(blueTag.id) > -1, 'the id was added to the fks array'); 31 | assert.ok(blueTag.labels.includes(tag), 'the inverse was set'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/4-named-reflexive/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`a ${state} can update its association to include a saved child via childIds`, function(assert) { 12 | let [ tag, originalTags ] = this.helper[state](); 13 | let savedTag = this.helper.savedChild(); 14 | 15 | tag.labelIds = [ savedTag.id ]; 16 | 17 | assert.deepEqual(tag.labels.models[0].attrs, savedTag.attrs); 18 | assert.deepEqual(tag.labelIds, [ savedTag.id ]); 19 | 20 | tag.save(); 21 | savedTag.reload(); 22 | 23 | assert.deepEqual(savedTag.labels.models[0].attrs, tag.attrs, 'the inverse was set'); 24 | originalTags.forEach(originalTag => { 25 | if (originalTag.isSaved()) { 26 | originalTag.reload(); 27 | assert.notOk(originalTag.labels.includes(tag), 'old inverses were cleared'); 28 | } 29 | }); 30 | }); 31 | 32 | test(`a ${state} can clear its association via a null childIds`, function(assert) { 33 | let [ tag, originalTags ] = this.helper[state](); 34 | 35 | tag.labelIds = null; 36 | 37 | assert.deepEqual(tag.labels.models, []); 38 | assert.deepEqual(tag.labelIds, []); 39 | 40 | tag.save(); 41 | originalTags.forEach(originalTag => { 42 | originalTag.reload(); 43 | assert.notOk(originalTag.labels.includes(tag), 'old inverses were cleared'); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/4-named-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ tag, labels ] = this.helper[state](); 13 | 14 | if (labels && labels.length) { 15 | labels.forEach(t => t.destroy()); 16 | tag.reload(); 17 | } 18 | 19 | assert.equal(tag.labels.length, 0); 20 | assert.equal(tag.labelIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/5-named-reflexive-explicit-inverse/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive Explicit Inverse | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`the references of a ${state} are correct`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | assert.equal(tag.labels.models.length, tags.length, 'the parent has the correct number of children'); 15 | assert.equal(tag.labelIds.length, tags.length, 'the parent has the correct number of children ids'); 16 | 17 | tags.forEach((t, i) => { 18 | assert.deepEqual(tag.labels.models[i], t, 'each child is in parent.children array'); 19 | 20 | if (t.isSaved()) { 21 | assert.ok(tag.labelIds.indexOf(t.id) > -1, 'each saved child id is in parent.childrenIds array'); 22 | } 23 | 24 | // Check the inverse 25 | assert.ok(t.labels.includes(tag)); 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/5-named-reflexive-explicit-inverse/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive Explicit Inverse | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated child`, function(assert) { 15 | let [ tag ] = this.helper[state](); 16 | let initialCount = tag.labels.models.length; 17 | 18 | let orangeTag = tag.createLabel({ name: 'Orange' }); 19 | 20 | assert.ok(orangeTag.id, 'the child was persisted'); 21 | assert.equal(tag.labels.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(tag.labels.includes(orangeTag), 'the model was added to tag.labels'); 23 | assert.ok(tag.labelIds.indexOf(orangeTag.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(tag.attrs.labelIds.indexOf(orangeTag.id) > -1, 'fks were persisted'); 25 | assert.ok(orangeTag.labels.includes(tag), 'the inverse was set'); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/5-named-reflexive-explicit-inverse/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive Explicit Inverse | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated child`, function(assert) { 16 | let [ tag ] = this.helper[state](); 17 | let initialCount = tag.labels.models.length; 18 | 19 | let blueTag = tag.newLabel({ name: 'Blue' }); 20 | 21 | assert.ok(!blueTag.id, 'the child was not persisted'); 22 | assert.equal(tag.labels.models.length, initialCount + 1); 23 | assert.equal(blueTag.labels.models.length, 1, 'the inverse was set'); 24 | 25 | blueTag.save(); 26 | 27 | assert.deepEqual(blueTag.attrs, { id: blueTag.id, name: 'Blue', labelIds: [ tag.id ] }, 'the child was persisted'); 28 | assert.equal(tag.labels.models.length, initialCount + 1, 'the collection size was increased'); 29 | assert.ok(tag.labels.includes(blueTag), 'the model was added to tag.labels'); 30 | assert.ok(tag.labelIds.indexOf(blueTag.id) > -1, 'the id was added to the fks array'); 31 | assert.ok(blueTag.labels.includes(tag), 'the inverse was set'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/5-named-reflexive-explicit-inverse/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive Explicit Inverse | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`a ${state} can update its association to include a saved child via childIds`, function(assert) { 12 | let [ tag, originalTags ] = this.helper[state](); 13 | let savedTag = this.helper.savedChild(); 14 | 15 | tag.labelIds = [ savedTag.id ]; 16 | 17 | assert.deepEqual(tag.labels.models[0].attrs, savedTag.attrs); 18 | assert.deepEqual(tag.labelIds, [ savedTag.id ]); 19 | 20 | tag.save(); 21 | savedTag.reload(); 22 | 23 | assert.deepEqual(savedTag.labels.models[0].attrs, tag.attrs, 'the inverse was set'); 24 | originalTags.forEach(originalTag => { 25 | if (originalTag.isSaved()) { 26 | originalTag.reload(); 27 | assert.notOk(originalTag.labels.includes(tag), 'old inverses were cleared'); 28 | } 29 | }); 30 | }); 31 | 32 | test(`a ${state} can clear its association via a null childIds`, function(assert) { 33 | let [ tag, originalTags ] = this.helper[state](); 34 | 35 | tag.labelIds = null; 36 | 37 | assert.deepEqual(tag.labels.models, []); 38 | assert.deepEqual(tag.labelIds, []); 39 | 40 | tag.save(); 41 | originalTags.forEach(originalTag => { 42 | originalTag.reload(); 43 | assert.notOk(originalTag.labels.includes(tag), 'old inverses were cleared'); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/5-named-reflexive-explicit-inverse/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named Reflexive Explicit Inverse | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ tag, labels ] = this.helper[state](); 13 | 14 | if (labels && labels.length) { 15 | labels.forEach(t => t.destroy()); 16 | tag.reload(); 17 | } 18 | 19 | assert.equal(tag.labels.length, 0); 20 | assert.equal(tag.labelIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/6-one-way-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-Way Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`the references of a ${state} are correct`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | assert.equal(tag.tags.models.length, tags.length, 'the parent has the correct number of children'); 15 | assert.equal(tag.tagIds.length, tags.length, 'the parent has the correct number of children ids'); 16 | 17 | tags.forEach((t, i) => { 18 | assert.deepEqual(tag.tags.models[i], t, 'each child is in parent.children array'); 19 | 20 | if (t.isSaved()) { 21 | assert.ok(tag.tagIds.indexOf(t.id) > -1, 'each saved child id is in parent.childrenIds array'); 22 | } 23 | }); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/6-one-way-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-Way Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated child`, function(assert) { 15 | let [ tag ] = this.helper[state](); 16 | let initialCount = tag.tags.models.length; 17 | 18 | let orangeTag = tag.createTag({ name: 'Orange' }); 19 | 20 | assert.ok(orangeTag.id, 'the child was persisted'); 21 | assert.equal(tag.tags.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(tag.tags.includes(orangeTag), 'the model was added to tag.tags'); 23 | assert.ok(tag.tagIds.indexOf(orangeTag.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(tag.attrs.tagIds.indexOf(orangeTag.id) > -1, 'fks were persisted'); 25 | assert.notOk(orangeTag.tags.includes(tag), 'the inverse was not set'); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/6-one-way-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-Way Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated child`, function(assert) { 16 | let [ tag ] = this.helper[state](); 17 | let initialCount = tag.tags.models.length; 18 | 19 | let blueTag = tag.newTag({ name: 'Blue' }); 20 | 21 | assert.ok(!blueTag.id, 'the child was not persisted'); 22 | assert.equal(tag.tags.models.length, initialCount + 1); 23 | assert.equal(blueTag.tags.models.length, 0, 'the inverse was not set'); 24 | 25 | blueTag.save(); 26 | 27 | assert.deepEqual(blueTag.attrs, { id: blueTag.id, name: 'Blue', tagIds: [ ] }, 'the child was persisted'); 28 | assert.equal(tag.tags.models.length, initialCount + 1, 'the collection size was increased'); 29 | assert.ok(tag.tags.includes(blueTag), 'the model was added to tag.tags'); 30 | assert.ok(tag.tagIds.indexOf(blueTag.id) > -1, 'the id was added to the fks array'); 31 | assert.notOk(blueTag.tags.includes(tag), 'the inverse was not set'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/6-one-way-reflexive/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-Way Reflexive | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`a ${state} can update its association to include a saved child via childIds`, function(assert) { 12 | let [ tag ] = this.helper[state](); 13 | let savedTag = this.helper.savedChild(); 14 | 15 | tag.tagIds = [ savedTag.id ]; 16 | 17 | assert.deepEqual(tag.tags.models[0].attrs, savedTag.attrs); 18 | assert.deepEqual(tag.tagIds, [ savedTag.id ]); 19 | 20 | tag.save(); 21 | savedTag.reload(); 22 | 23 | assert.equal(savedTag.tags.models.length, 0, 'the inverse was not set'); 24 | }); 25 | 26 | test(`a ${state} can clear its association via a null childIds`, function(assert) { 27 | let [ tag ] = this.helper[state](); 28 | 29 | tag.tagIds = null; 30 | 31 | assert.deepEqual(tag.tags.models, []); 32 | assert.deepEqual(tag.tagIds, []); 33 | 34 | tag.save(); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/6-one-way-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-Way Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | if (tags && tags.length) { 15 | tags.forEach(t => t.destroy()); 16 | tag.reload(); 17 | } 18 | 19 | assert.equal(tag.tags.length, 0); 20 | assert.equal(tag.tagIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/7-named-one-way-reflexive/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named One-Way Reflexive | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`the references of a ${state} are correct`, function(assert) { 12 | let [ tag, tags ] = this.helper[state](); 13 | 14 | assert.equal(tag.labels.models.length, tags.length, 'the parent has the correct number of children'); 15 | assert.equal(tag.labelIds.length, tags.length, 'the parent has the correct number of children ids'); 16 | 17 | tags.forEach((t, i) => { 18 | assert.deepEqual(tag.labels.models[i], t, 'each child is in parent.children array'); 19 | 20 | if (t.isSaved()) { 21 | assert.ok(tag.labelIds.indexOf(t.id) > -1, 'each saved child id is in parent.childrenIds array'); 22 | } 23 | }); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/7-named-one-way-reflexive/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named One-Way Reflexive | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated child`, function(assert) { 15 | let [ tag ] = this.helper[state](); 16 | let initialCount = tag.labels.models.length; 17 | 18 | let orangeTag = tag.createLabel({ name: 'Orange' }); 19 | 20 | assert.ok(orangeTag.id, 'the child was persisted'); 21 | assert.equal(tag.labels.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(tag.labels.includes(orangeTag), 'the model was added to tag.labels'); 23 | assert.ok(tag.labelIds.indexOf(orangeTag.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(tag.attrs.labelIds.indexOf(orangeTag.id) > -1, 'fks were persisted'); 25 | assert.notOk(orangeTag.labels.includes(tag), 'the inverse was not set'); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/7-named-one-way-reflexive/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named One-Way Reflexive | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated child`, function(assert) { 16 | let [ tag ] = this.helper[state](); 17 | let initialCount = tag.labels.models.length; 18 | 19 | let blueTag = tag.newLabel({ name: 'Blue' }); 20 | 21 | assert.ok(!blueTag.id, 'the child was not persisted'); 22 | assert.equal(tag.labels.models.length, initialCount + 1); 23 | assert.equal(blueTag.labels.models.length, 0, 'the inverse was not set'); 24 | 25 | blueTag.save(); 26 | 27 | assert.deepEqual(blueTag.attrs, { id: blueTag.id, name: 'Blue', labelIds: [ ] }, 'the child was persisted'); 28 | assert.equal(tag.labels.models.length, initialCount + 1, 'the collection size was increased'); 29 | assert.ok(tag.labels.includes(blueTag), 'the model was added to tag.labels'); 30 | assert.ok(tag.labelIds.indexOf(blueTag.id) > -1, 'the id was added to the fks array'); 31 | assert.notOk(blueTag.labels.includes(tag), 'the inverse was not set'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/7-named-one-way-reflexive/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named One-Way Reflexive | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`a ${state} can update its association to include a saved child via childIds`, function(assert) { 12 | let [ tag ] = this.helper[state](); 13 | let savedTag = this.helper.savedChild(); 14 | 15 | tag.labelIds = [ savedTag.id ]; 16 | 17 | assert.deepEqual(tag.labels.models[0].attrs, savedTag.attrs); 18 | assert.deepEqual(tag.labelIds, [ savedTag.id ]); 19 | 20 | tag.save(); 21 | savedTag.reload(); 22 | 23 | assert.equal(savedTag.labels.models.length, 0, 'the inverse was not set'); 24 | }); 25 | 26 | test(`a ${state} can clear its association via a null childIds`, function(assert) { 27 | let [ tag ] = this.helper[state](); 28 | 29 | tag.labelIds = null; 30 | 31 | assert.deepEqual(tag.labels.models, []); 32 | assert.deepEqual(tag.labelIds, []); 33 | 34 | tag.save(); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/7-named-one-way-reflexive/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Named One-Way Reflexive | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ tag, labels ] = this.helper[state](); 13 | 14 | if (labels && labels.length) { 15 | labels.forEach(t => t.destroy()); 16 | tag.reload(); 17 | } 18 | 19 | assert.equal(tag.labels.length, 0); 20 | assert.equal(tag.labelIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/8-many-to-many/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many to Many | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`the references of a ${state} are correct`, function(assert) { 12 | let [ order, products ] = this.helper[state](); 13 | 14 | assert.equal(order.products.models.length, products.length, 'the parent has the correct number of children'); 15 | assert.equal(order.productIds.length, products.length, 'the parent has the correct number of children ids'); 16 | 17 | products.forEach((p, i) => { 18 | assert.deepEqual(order.products.models[i], p, 'each child is in parent.children array'); 19 | 20 | if (p.isSaved()) { 21 | assert.ok(order.productIds.indexOf(p.id) > -1, 'each saved child id is in parent.childrenIds array'); 22 | } 23 | 24 | // Check the inverse 25 | assert.ok(p.orders.includes(order)); 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/8-many-to-many/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many to Many | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated child`, function(assert) { 15 | let [ order ] = this.helper[state](); 16 | let initialCount = order.products.models.length; 17 | 18 | let orangeProduct = order.createProduct({ name: 'Orange' }); 19 | 20 | assert.ok(orangeProduct.id, 'the child was persisted'); 21 | assert.equal(order.products.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(order.products.includes(orangeProduct), 'the model was added to order.products'); 23 | assert.ok(order.productIds.indexOf(orangeProduct.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(order.attrs.productIds.indexOf(orangeProduct.id) > -1, 'fks were persisted'); 25 | assert.ok(orangeProduct.orders.includes(order), 'the inverse was set'); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/8-many-to-many/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many to Many | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated child`, function(assert) { 16 | let [ order ] = this.helper[state](); 17 | let initialCount = order.products.models.length; 18 | 19 | let blueProduct = order.newProduct({ name: 'Blue' }); 20 | 21 | assert.ok(!blueProduct.id, 'the child was not persisted'); 22 | assert.equal(order.products.models.length, initialCount + 1); 23 | assert.equal(blueProduct.orders.models.length, 1, 'the inverse was set'); 24 | 25 | blueProduct.save(); 26 | 27 | assert.deepEqual(blueProduct.attrs, { id: blueProduct.id, name: 'Blue', orderIds: [ order.id ] }, 'the child was persisted'); 28 | assert.equal(order.products.models.length, initialCount + 1, 'the collection size was increased'); 29 | assert.ok(order.products.includes(blueProduct), 'the model was added to order.products'); 30 | assert.ok(order.productIds.indexOf(blueProduct.id) > -1, 'the id was added to the fks array'); 31 | assert.ok(blueProduct.orders.includes(order), 'the inverse was set'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/8-many-to-many/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many to Many | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`a ${state} can update its association to include a saved child via childIds`, function(assert) { 12 | let [ order, originalProducts ] = this.helper[state](); 13 | let savedProduct = this.helper.savedChild(); 14 | 15 | order.productIds = [ savedProduct.id ]; 16 | 17 | assert.deepEqual(order.products.models[0].attrs, savedProduct.attrs); 18 | assert.deepEqual(order.productIds, [ savedProduct.id ]); 19 | 20 | order.save(); 21 | savedProduct.reload(); 22 | 23 | assert.deepEqual(savedProduct.orders.models[0].attrs, order.attrs, 'the inverse was set'); 24 | originalProducts.forEach(p => { 25 | if (p.isSaved()) { 26 | p.reload(); 27 | assert.notOk(p.orders.includes(order), 'old inverses were cleared'); 28 | } 29 | }); 30 | }); 31 | 32 | test(`a ${state} can clear its association via a null childIds`, function(assert) { 33 | let [ order, originalProducts ] = this.helper[state](); 34 | 35 | order.productIds = null; 36 | 37 | assert.deepEqual(order.products.models, []); 38 | assert.deepEqual(order.productIds, []); 39 | 40 | order.save(); 41 | 42 | originalProducts.forEach(p => { 43 | p.reload(); 44 | assert.notOk(p.orders.includes(order), 'old inverses were cleared'); 45 | }); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/8-many-to-many/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | Many to Many | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ order, products ] = this.helper[state](); 13 | 14 | if (products && products.length) { 15 | products.forEach(t => t.destroy()); 16 | order.reload(); 17 | } 18 | 19 | assert.equal(order.products.length, 0); 20 | assert.equal(order.productIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/9-one-way-polymorphic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-way Polymorphic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, posts ] = this.helper[state](); 16 | 17 | assert.equal(user.things.models.length, posts.length, 'the parent has the correct number of children'); 18 | assert.equal(user.thingIds.length, posts.length, 'the parent has the correct number of children ids'); 19 | 20 | posts.forEach((post, i) => { 21 | assert.ok(user.things.includes(post), 'each child is in parent.children array'); 22 | 23 | if (post.isSaved()) { 24 | assert.deepEqual(user.thingIds[i], { type: 'post', id: post.id }, 'each saved child id is in parent.childrenIds array'); 25 | } 26 | }); 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/9-one-way-polymorphic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-way Polymorphic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let initialCount = user.things.models.length; 17 | 18 | let post = user.createThing('post', { title: 'Lorem ipsum' }); 19 | 20 | assert.ok(post.id, 'the child was persisted'); 21 | assert.equal(user.things.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(user.things.includes(post), 'the model was added to user.posts'); 23 | assert.ok(user.thingIds.find(obj => { 24 | return (obj.id === post.id && obj.type === 'post'); 25 | }), 'the id was added to the fks array'); 26 | assert.ok(user.attrs.thingIds.find(obj => { 27 | return (obj.id === post.id && obj.type === 'post'); 28 | }), 'fks were persisted'); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/9-one-way-polymorphic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-way Polymorphic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | let initialCount = user.things.models.length; 18 | 19 | let post = user.newThing('post', { title: 'Lorem ipsum' }); 20 | 21 | assert.ok(!post.id, 'the child was not persisted'); 22 | assert.equal(user.things.models.length, initialCount + 1); 23 | 24 | post.save(); 25 | 26 | assert.deepEqual(post.attrs, { id: post.id, title: 'Lorem ipsum' }, 'the child was persisted'); 27 | assert.equal(user.things.models.length, initialCount + 1, 'the collection size was increased'); 28 | assert.ok(user.things.includes(post), 'the model was added to user.things'); 29 | assert.ok(user.thingIds.find(obj => { 30 | return (obj.id === post.id && obj.type === 'post'); 31 | }), 'the id was added to the fks array'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/9-one-way-polymorphic/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-way Polymorphic | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.thingIds = [ { type: 'post', id: savedPost.id } ]; 19 | 20 | assert.ok(user.things.includes(savedPost)); 21 | assert.ok(user.thingIds.find(({ id, type }) => ((id === savedPost.id && type === 'post')))); 22 | }); 23 | 24 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 25 | let [ user ] = this.helper[state](); 26 | 27 | user.thingIds = null; 28 | 29 | assert.deepEqual(user.things.models, []); 30 | assert.deepEqual(user.thingIds, []); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/has-many/9-one-way-polymorphic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Has Many | One-way Polymorphic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ user, posts ] = this.helper[state](); 13 | 14 | if (posts && posts.length) { 15 | posts.forEach(p => p.destroy()); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.things.length, 0); 20 | assert.equal(user.thingIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/1-one-to-many/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, posts ] = this.helper[state](); 16 | 17 | assert.equal(user.posts.models.length, posts.length, 'the parent has the correct number of children'); 18 | assert.equal(user.postIds.length, posts.length, 'the parent has the correct number of children ids'); 19 | 20 | posts.forEach((post, i) => { 21 | assert.deepEqual(user.posts.models[i], posts[i], 'each child is in parent.children array'); 22 | 23 | if (post.isSaved()) { 24 | assert.ok(user.postIds.indexOf(post.id) > -1, 'each saved child id is in parent.childrenIds array'); 25 | } 26 | 27 | // Check the inverse 28 | assert.deepEqual(post.user.attrs, user.attrs); 29 | assert.deepEqual(post.userId, user.id); 30 | }); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/1-one-to-many/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let initialCount = user.posts.models.length; 17 | 18 | let post = user.createPost({ title: 'Lorem ipsum' }); 19 | 20 | assert.ok(post.id, 'the child was persisted'); 21 | assert.equal(user.posts.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(user.posts.includes(post), 'the model was added to user.posts'); 23 | assert.ok(user.postIds.indexOf(post.id) > -1, 'the id was added to the fks array'); 24 | assert.ok(user.attrs.postIds.indexOf(post.id) > -1, 'fks were persisted'); 25 | 26 | // Check the inverse 27 | assert.deepEqual(post.user.attrs, user.attrs); 28 | assert.deepEqual(post.userId, user.id); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/1-one-to-many/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | let initialCount = user.posts.models.length; 18 | 19 | let post = user.newPost({ title: 'Lorem ipsum' }); 20 | 21 | assert.ok(!post.id, 'the child was not persisted'); 22 | assert.equal(user.posts.models.length, initialCount + 1); 23 | 24 | post.save(); 25 | 26 | assert.deepEqual(post.attrs, { id: post.id, title: 'Lorem ipsum', userId: user.id }, 'the child was persisted'); 27 | assert.equal(user.posts.models.length, initialCount + 1, 'the collection size was increased'); 28 | assert.ok(user.posts.includes(post), 'the model was added to user.posts'); 29 | assert.ok(user.postIds.indexOf(post.id) > -1, 'the id was added to the fks array'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/1-one-to-many/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ user, originalPosts ] = this.helper[state](); 16 | let savedPost = this.helper.savedChild(); 17 | 18 | user.postIds = [ savedPost.id ]; 19 | 20 | assert.ok(user.posts.includes(savedPost)); 21 | assert.deepEqual(user.postIds, [ savedPost.id ]); 22 | 23 | user.save(); 24 | savedPost.reload(); 25 | 26 | // Check the inverse 27 | assert.deepEqual(savedPost.user.attrs, user.attrs); 28 | assert.equal(savedPost.userId, user.id); 29 | 30 | // Check old associates 31 | originalPosts.forEach(post => { 32 | if (post.isSaved()) { 33 | post.reload(); 34 | assert.equal(post.user, null); 35 | } 36 | }); 37 | }); 38 | 39 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 40 | let [ user, originalPosts ] = this.helper[state](); 41 | 42 | user.postIds = null; 43 | 44 | assert.deepEqual(user.posts.models, []); 45 | assert.deepEqual(user.postIds, []); 46 | 47 | user.save(); 48 | 49 | // Check old associates 50 | originalPosts.forEach(post => { 51 | if (post.isSaved()) { 52 | post.reload(); 53 | assert.equal(post.user, null); 54 | } 55 | }); 56 | }); 57 | }); 58 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/2-many-to-one/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | Many To One | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ post, user ] = this.helper[state](); 16 | 17 | assert.deepEqual(post.user, user); 18 | assert.equal(post.userId, user ? user.id : null); 19 | 20 | post.save(); 21 | 22 | // Check the inverse 23 | if (user && user.isSaved()) { 24 | user.reload(); 25 | assert.ok(user.posts.includes(post)); 26 | } 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/2-many-to-one/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | Many To One | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ post, originalUser ] = this.helper[state](); 16 | 17 | let user = post.createUser({ name: 'Zelda' }); 18 | 19 | assert.ok(user.id, 'the parent was persisted'); 20 | assert.deepEqual(post.user.attrs, user.attrs); 21 | assert.equal(post.userId, user.id); 22 | 23 | // Check the inverse 24 | assert.ok(user.posts.includes(post), 'the inverse was set'); 25 | 26 | // Ensure old inverse was cleared 27 | if (originalUser && originalUser.isSaved()) { 28 | originalUser.reload(); 29 | assert.notOk(originalUser.posts.includes(post)); 30 | } 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/2-many-to-one/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | Many To One | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ post, originalUser ] = this.helper[state](); 17 | 18 | let user = post.newUser({ name: 'Zelda' }); 19 | 20 | assert.ok(!user.id, 'the child was not persisted'); 21 | assert.deepEqual(post.user, user, 'the relationship was set'); 22 | assert.ok(user.posts.includes(post), 'the inverse was set'); 23 | 24 | user.save(); 25 | post.reload(); 26 | 27 | assert.ok(user.id, 'the parent was persisted'); 28 | assert.deepEqual(post.user.attrs, user.attrs); 29 | assert.equal(post.userId, user.id); 30 | 31 | // Check the inverse 32 | assert.ok(user.posts.includes(post), 'the inverse was set'); 33 | 34 | // Ensure old inverse was cleared 35 | if (originalUser && originalUser.isSaved()) { 36 | originalUser.reload(); 37 | assert.notOk(originalUser.posts.includes(post)); 38 | } 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/2-many-to-one/association-set-ids-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | Many To One | association #setIds', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can update its association via parentId, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can update its association to a saved parent via parentId`, function(assert) { 15 | let [ post, originalUser ] = this.helper[state](); 16 | let user = this.helper.savedParent(); 17 | 18 | post.userId = user.id; 19 | 20 | assert.equal(post.userId, user.id); 21 | assert.deepEqual(post.user.attrs, user.attrs); 22 | 23 | assert.ok(post.user.posts.includes(post), 'the inverse was set'); 24 | 25 | post.save(); 26 | user.reload(); 27 | 28 | assert.ok(user.posts.includes(post)); 29 | 30 | // Old inverses were cleared 31 | if (originalUser && originalUser.isSaved()) { 32 | originalUser.reload(); 33 | assert.notOk(originalUser.posts.includes(post)); 34 | } 35 | }); 36 | 37 | test(`a ${state} can clear its association via a null parentId`, function(assert) { 38 | let [ post, originalUser ] = this.helper[state](); 39 | 40 | post.userId = null; 41 | 42 | assert.deepEqual(post.user, null); 43 | assert.deepEqual(post.userId, null); 44 | 45 | post.save(); 46 | 47 | if (originalUser && originalUser.isSaved()) { 48 | originalUser.reload(); 49 | assert.notOk(originalUser.posts.includes(post)); 50 | } 51 | }); 52 | }); 53 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/3-one-to-many-polymorphic/accessor-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many Polymorphic | accessor', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The reference to a belongs-to association is correct, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`the references of a ${state} are correct`, function(assert) { 15 | let [ user, posts ] = this.helper[state](); 16 | 17 | assert.equal(user.things.models.length, posts.length, 'the parent has the correct number of children'); 18 | assert.equal(user.thingIds.length, posts.length, 'the parent has the correct number of children ids'); 19 | 20 | posts.forEach((post, i) => { 21 | assert.deepEqual(user.things.models[i], posts[i], 'each child is in parent.children array'); 22 | 23 | if (post.isSaved()) { 24 | assert.ok(user.thingIds.find(obj => { 25 | return (obj.id === post.id && obj.type === 'post'); 26 | }), 'each saved child id is in parent.childrenIds array'); 27 | } 28 | 29 | // Check the inverse 30 | assert.deepEqual(post.user.attrs, user.attrs); 31 | assert.deepEqual(post.userId, user.id); 32 | }); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/3-one-to-many-polymorphic/association-create-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many Polymorphic | association #create', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can create a has-many association, for all states 12 | */ 13 | states.forEach((state) => { 14 | test(`a ${state} can create an associated parent`, function(assert) { 15 | let [ user ] = this.helper[state](); 16 | let initialCount = user.things.models.length; 17 | 18 | let post = user.createThing('post', { title: 'Lorem ipsum' }); 19 | 20 | assert.ok(post.id, 'the child was persisted'); 21 | assert.equal(user.things.models.length, initialCount + 1, 'the collection size was increased'); 22 | assert.ok(user.things.includes(post), 'the model was added to user.posts'); 23 | assert.ok(user.thingIds.find(obj => { 24 | return (obj.id === post.id && obj.type === 'post'); 25 | }), 'the id was added to the fks array'); 26 | assert.ok(user.attrs.thingIds.find(obj => { 27 | return (obj.id === post.id && obj.type === 'post'); 28 | }), 'fks were persisted'); 29 | 30 | // Check the inverse 31 | assert.deepEqual(post.user.attrs, user.attrs); 32 | assert.deepEqual(post.userId, user.id); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/3-one-to-many-polymorphic/association-new-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many Polymorphic | association #new', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | /* 11 | The model can make a new unsaved belongs-to association, for all states 12 | */ 13 | 14 | states.forEach((state) => { 15 | test(`a ${state} can build a new associated parent`, function(assert) { 16 | let [ user ] = this.helper[state](); 17 | let initialCount = user.things.models.length; 18 | 19 | let post = user.newThing('post', { title: 'Lorem ipsum' }); 20 | 21 | assert.ok(!post.id, 'the child was not persisted'); 22 | assert.equal(user.things.models.length, initialCount + 1); 23 | 24 | post.save(); 25 | 26 | assert.deepEqual(post.attrs, { id: post.id, title: 'Lorem ipsum', userId: user.id }, 'the child was persisted'); 27 | assert.equal(user.things.models.length, initialCount + 1, 'the collection size was increased'); 28 | assert.ok(user.things.includes(post), 'the model was added to user.things'); 29 | assert.ok(user.thingIds.find(obj => { 30 | return (obj.id === post.id && obj.type === 'post'); 31 | }), 'the id was added to the fks array'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /tests/integration/orm/mixed/3-one-to-many-polymorphic/delete-test.js: -------------------------------------------------------------------------------- 1 | import Helper, { states } from './_helper'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | ORM | Mixed | One To Many Polymorphic | delete', { 5 | beforeEach() { 6 | this.helper = new Helper(); 7 | } 8 | }); 9 | 10 | states.forEach((state) => { 11 | test(`deleting children updates the parent's foreign key for a ${state}`, function(assert) { 12 | let [ user, posts ] = this.helper[state](); 13 | 14 | if (posts && posts.length) { 15 | posts.forEach(p => p.destroy()); 16 | user.reload(); 17 | } 18 | 19 | assert.equal(user.things.length, 0); 20 | assert.equal(user.thingIds.length, 0); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/integration/orm/reinitialize-associations-test.js: -------------------------------------------------------------------------------- 1 | // jscs:disable disallowVar 2 | import { Model, hasMany, Db, Schema } from '@bigtest/mirage'; 3 | import {module, test} from 'qunit'; 4 | 5 | // Model classes are defined statically, just like in a typical app 6 | var User = Model.extend({ 7 | addresses: hasMany() 8 | }); 9 | var Address = Model.extend(); 10 | 11 | module('Integration | ORM | reinitialize associations', { 12 | beforeEach() { 13 | this.schema = new Schema(new Db(), { 14 | address: Address, 15 | user: User 16 | }); 17 | 18 | this.schema.addresses.create({ id: 1, country: 'Hyrule' }); 19 | this.schema.users.create({ id: 1, name: 'Link', addressIds: [ 1 ] }); 20 | } 21 | }); 22 | 23 | // By running two tests, we force the statically-defined classes to be 24 | // registered twice. 25 | test('safely initializes associations', function(assert) { 26 | assert.equal(this.schema.users.find(1).addresses.models[0].country, 'Hyrule'); 27 | }); 28 | test('safely initializes associations again', function(assert) { 29 | assert.equal(this.schema.users.find(1).addresses.models[0].country, 'Hyrule'); 30 | }); 31 | -------------------------------------------------------------------------------- /tests/integration/orm/where-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Db, Model, Collection } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | let schema; 5 | let User = Model.extend(); 6 | module('Integration | ORM | #where', { 7 | beforeEach() { 8 | let db = new Db({ users: [ 9 | { id: 1, name: 'Link', good: true }, 10 | { id: 2, name: 'Zelda', good: true }, 11 | { id: 3, name: 'Ganon', good: false } 12 | ] }); 13 | 14 | schema = new Schema(db, { 15 | user: User 16 | }); 17 | } 18 | }); 19 | 20 | test('it returns models that match a query with where', function(assert) { 21 | let users = schema.users.where({ good: false }); 22 | 23 | assert.ok(users instanceof Collection, 'it returns a collection'); 24 | assert.equal(users.models.length, 1); 25 | assert.ok(users.models[0] instanceof User); 26 | assert.deepEqual(users.models[0].attrs, { id: '3', name: 'Ganon', good: false }); 27 | }); 28 | 29 | test('it returns models that match using a query function', function(assert) { 30 | let users = schema.users.where(function(rec) { 31 | return !rec.good; 32 | }); 33 | 34 | assert.ok(users instanceof Collection, 'it returns a collection'); 35 | assert.equal(users.models.length, 1); 36 | assert.ok(users.models[0] instanceof User); 37 | assert.deepEqual(users.models[0].attrs, { id: '3', name: 'Ganon', good: false }); 38 | }); 39 | 40 | test('it returns an empty collection if no models match a query', function(assert) { 41 | let users = schema.users.where({ name: 'Link', good: false }); 42 | 43 | assert.ok(users instanceof Collection, 'it returns a collection'); 44 | assert.equal(users.models.length, 0); 45 | }); 46 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/associations/polymorphic-belongs-to-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry, Model, belongsTo, Db, Schema } from '@bigtest/mirage'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | Serializers | Base | Associations | Polymorphic Belongs To', { 5 | beforeEach() { 6 | this.schema = new Schema(new Db(), { 7 | post: Model.extend(), 8 | comment: Model.extend({ 9 | commentable: belongsTo({ polymorphic: true }) 10 | }) 11 | }); 12 | 13 | let post = this.schema.posts.create({ title: 'Lorem ipsum' }); 14 | this.schema.comments.create({ commentable: post, text: 'Foo' }); 15 | 16 | this.BaseSerializer = Serializer.extend({ 17 | embed: false 18 | }); 19 | }, 20 | 21 | afterEach() { 22 | this.schema.db.emptyData(); 23 | } 24 | }); 25 | 26 | test(`it can serialize a polymorphic belongs-to relationship`, function(assert) { 27 | let registry = new SerializerRegistry(this.schema, { 28 | application: this.BaseSerializer, 29 | comment: this.BaseSerializer.extend({ 30 | include: ['commentable'] 31 | }) 32 | }); 33 | 34 | let comment = this.schema.comments.find(1); 35 | let result = registry.serialize(comment); 36 | 37 | assert.deepEqual(result, { 38 | comment: { 39 | id: '1', 40 | text: 'Foo', 41 | commentableType: 'post', 42 | commentableId: '1' 43 | }, 44 | posts: [ 45 | { id: '1', title: 'Lorem ipsum' } 46 | ] 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/associations/polymorphic-has-many-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry, Model, hasMany, Db, Schema } from '@bigtest/mirage'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Integration | Serializers | Base | Associations | Polymorphic Has Many', { 5 | beforeEach() { 6 | this.schema = new Schema(new Db(), { 7 | user: Model.extend({ 8 | things: hasMany({ polymorphic: true }) 9 | }), 10 | picture: Model.extend() 11 | }); 12 | 13 | let post = this.schema.pictures.create({ title: 'Lorem ipsum' }); 14 | this.schema.users.create({ things: [ post ], name: 'Ned' }); 15 | 16 | this.BaseSerializer = Serializer.extend({ 17 | embed: false 18 | }); 19 | }, 20 | 21 | afterEach() { 22 | this.schema.db.emptyData(); 23 | } 24 | }); 25 | 26 | test(`it can serialize a polymorphic has-many relationship`, function(assert) { 27 | let registry = new SerializerRegistry(this.schema, { 28 | application: this.BaseSerializer, 29 | user: this.BaseSerializer.extend({ 30 | include: ['things'] 31 | }) 32 | }); 33 | 34 | let user = this.schema.users.find(1); 35 | let result = registry.serialize(user); 36 | 37 | assert.deepEqual(result, { 38 | user: { 39 | id: '1', 40 | name: 'Ned', 41 | things: [ 42 | { id: '1', type: 'picture' } 43 | ] 44 | }, 45 | pictures: [ 46 | { id: '1', title: 'Lorem ipsum' } 47 | ] 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/assorted-collections-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry } from '@bigtest/mirage'; 2 | import schemaHelper from '../schema-helper'; 3 | import {module, test} from 'qunit'; 4 | 5 | module('Integration | Serializers | Base | Assorted Collections', { 6 | beforeEach() { 7 | this.schema = schemaHelper.setup(); 8 | this.registry = new SerializerRegistry(this.schema, { 9 | greatPhoto: Serializer.extend({ 10 | attrs: ['id', 'title'] 11 | }) 12 | }); 13 | this.wordSmiths = [ 14 | { id: '1', name: 'Link' }, 15 | { id: '2', name: 'Zelda' }, 16 | { id: '3', name: 'Epona' } 17 | ]; 18 | this.greatPhotos = [ 19 | { id: '1', title: 'Amazing', location: 'Hyrule' }, 20 | { id: '2', title: 'greatPhoto', location: 'Goron City' } 21 | ]; 22 | this.schema.db.loadData({ 23 | wordSmiths: this.wordSmiths, 24 | greatPhotos: this.greatPhotos 25 | }); 26 | }, 27 | afterEach() { 28 | this.schema.db.emptyData(); 29 | } 30 | }); 31 | 32 | test(`an array of assorted collections can be serialized`, function(assert) { 33 | let result = this.registry.serialize([this.schema.wordSmiths.all(), this.schema.greatPhotos.all()]); 34 | 35 | assert.deepEqual(result, { 36 | wordSmiths: this.wordSmiths, 37 | greatPhotos: this.greatPhotos.map((attrs) => { 38 | delete attrs.location; 39 | return attrs; 40 | }) 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/attribute-key-formatting-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry, camelize } from '@bigtest/mirage'; 2 | import schemaHelper from '../schema-helper'; 3 | import {module, test} from 'qunit'; 4 | 5 | module('Integration | Serializers | Base | Attribute Key Formatting', { 6 | beforeEach() { 7 | this.schema = schemaHelper.setup(); 8 | this.registry = new SerializerRegistry(this.schema, { 9 | wordSmith: Serializer.extend({ 10 | keyForAttribute(key) { 11 | return camelize(key); 12 | } 13 | }) 14 | }); 15 | }, 16 | afterEach() { 17 | this.schema.db.emptyData(); 18 | } 19 | }); 20 | 21 | test(`keyForAttribute formats the attributes of a model`, function(assert) { 22 | let wordSmith = this.schema.wordSmiths.create({ 23 | id: 1, 24 | 'first-name': 'Link', 25 | 'last-name': 'Jackson', 26 | age: 323 27 | }); 28 | 29 | let result = this.registry.serialize(wordSmith); 30 | 31 | assert.deepEqual(result, { 32 | wordSmith: { 33 | id: '1', 34 | firstName: 'Link', 35 | lastName: 'Jackson', 36 | age: 323 37 | } 38 | }); 39 | }); 40 | 41 | test(`keyForAttribute also formats the models in a collections`, function(assert) { 42 | this.schema.wordSmiths.create({ id: 1, 'first-name': 'Link', 'last-name': 'Jackson' }); 43 | this.schema.wordSmiths.create({ id: 2, 'first-name': 'Zelda', 'last-name': 'Brown' }); 44 | let wordSmiths = this.schema.wordSmiths.all(); 45 | 46 | let result = this.registry.serialize(wordSmiths); 47 | 48 | assert.deepEqual(result, { 49 | wordSmiths: [ 50 | { id: '1', firstName: 'Link', lastName: 'Jackson' }, 51 | { id: '2', firstName: 'Zelda', lastName: 'Brown' } 52 | ] 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/attrs-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry } from '@bigtest/mirage'; 2 | import schemaHelper from '../schema-helper'; 3 | import { module, test } from 'qunit'; 4 | 5 | module('Integration | Serializers | Base | Attrs List', { 6 | beforeEach() { 7 | this.schema = schemaHelper.setup(); 8 | this.registry = new SerializerRegistry(this.schema, { 9 | wordSmith: Serializer.extend({ 10 | attrs: ['id', 'name'] 11 | }) 12 | }); 13 | }, 14 | afterEach() { 15 | this.schema.db.emptyData(); 16 | } 17 | }); 18 | 19 | test(`it returns only the whitelisted attrs when serializing a model`, function(assert) { 20 | let wordSmith = this.schema.wordSmiths.create({ 21 | id: 1, 22 | name: 'Link', 23 | age: 123 24 | }); 25 | 26 | let result = this.registry.serialize(wordSmith); 27 | assert.deepEqual(result, { 28 | wordSmith: { 29 | id: '1', 30 | name: 'Link' 31 | } 32 | }); 33 | }); 34 | 35 | test(`it returns only the whitelisted attrs when serializing a collection`, function(assert) { 36 | let { schema } = this; 37 | schema.wordSmiths.create({ id: 1, name: 'Link', age: 123 }); 38 | schema.wordSmiths.create({ id: 2, name: 'Zelda', age: 456 }); 39 | 40 | let collection = this.schema.wordSmiths.all(); 41 | let result = this.registry.serialize(collection); 42 | 43 | assert.deepEqual(result, { 44 | wordSmiths: [ 45 | { id: '1', name: 'Link' }, 46 | { id: '2', name: 'Zelda' } 47 | ] 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/root-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry } from '@bigtest/mirage'; 2 | import schemaHelper from '../schema-helper'; 3 | import { module, test } from 'qunit'; 4 | 5 | module('Integration | Serializers | Base | Root', { 6 | beforeEach() { 7 | this.schema = schemaHelper.setup(); 8 | this.registry = new SerializerRegistry(this.schema, { 9 | wordSmith: Serializer.extend({ 10 | embed: true, 11 | root: false 12 | }) 13 | }); 14 | }, 15 | afterEach() { 16 | this.schema.db.emptyData(); 17 | } 18 | }); 19 | 20 | test(`if root is false, it serializes a model by returning its attrs`, function(assert) { 21 | let wordSmith = this.schema.wordSmiths.create({ 22 | id: '1', 23 | name: 'Link' 24 | }); 25 | 26 | let result = this.registry.serialize(wordSmith); 27 | assert.deepEqual(result, { 28 | id: '1', 29 | name: 'Link' 30 | }); 31 | }); 32 | 33 | test(`if root is false, it serializes a collection of models by returning an array of their attrs`, function(assert) { 34 | this.schema.wordSmiths.create({ id: 1, name: 'Link' }); 35 | this.schema.wordSmiths.create({ id: 2, name: 'Zelda' }); 36 | let wordSmiths = this.schema.wordSmiths.all(); 37 | 38 | let result = this.registry.serialize(wordSmiths); 39 | 40 | assert.deepEqual(result, [ 41 | { id: '1', name: 'Link' }, 42 | { id: '2', name: 'Zelda' } 43 | ]); 44 | }); 45 | 46 | test(`if root is false, it serializes an empty collection by returning an empty array`, function(assert) { 47 | let emptywordSmithCollection = this.schema.wordSmiths.all(); 48 | let result = this.registry.serialize(emptywordSmithCollection); 49 | 50 | assert.deepEqual(result, []); 51 | }); 52 | -------------------------------------------------------------------------------- /tests/integration/serializers/base/serialize-array-of-models-test.js: -------------------------------------------------------------------------------- 1 | import { Serializer, SerializerRegistry } from '@bigtest/mirage'; 2 | import schemaHelper from '../schema-helper'; 3 | import { module, test } from 'qunit'; 4 | 5 | module('Integration | Serializers | Base | Array of Models', { 6 | beforeEach() { 7 | this.schema = schemaHelper.setup(); 8 | this.schema.wordSmiths.create({ id: 1, title: 'Link' }); 9 | }, 10 | afterEach() { 11 | this.schema.db.emptyData(); 12 | } 13 | }); 14 | 15 | test(`it applies correct serializer when the response is an array of models`, function(assert) { 16 | assert.expect(1); 17 | 18 | let wordSmiths = this.schema.wordSmiths.all().filter(() => true); 19 | let registry = new SerializerRegistry(this.schema, { 20 | wordSmith: Serializer.extend({ 21 | serialize() { 22 | assert.ok('serializer ran'); 23 | return {}; 24 | } 25 | }) 26 | }); 27 | 28 | registry.serialize(wordSmiths); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/integration/serializers/schema-helper.js: -------------------------------------------------------------------------------- 1 | import { Schema, Model, Db, hasMany, belongsTo } from '@bigtest/mirage'; 2 | 3 | export default { 4 | 5 | setup() { 6 | return new Schema(new Db(), { 7 | wordSmith: Model.extend({ 8 | blogPosts: hasMany() 9 | }), 10 | blogPost: Model.extend({ 11 | wordSmith: belongsTo(), 12 | fineComments: hasMany() 13 | }), 14 | fineComment: Model.extend({ 15 | blogPost: belongsTo() 16 | }), 17 | greatPhoto: Model, 18 | 19 | foo: Model.extend({ 20 | bar: belongsTo() 21 | }), 22 | bar: Model.extend({ 23 | baz: belongsTo() 24 | }), 25 | baz: Model.extend({ 26 | quuxes: hasMany() 27 | }), 28 | quux: Model.extend({ 29 | zomgs: hasMany() 30 | }), 31 | zomg: Model.extend({ 32 | lol: belongsTo() 33 | }), 34 | lol: Model 35 | }); 36 | } 37 | 38 | }; 39 | -------------------------------------------------------------------------------- /tests/integration/server-with-orm-test.js: -------------------------------------------------------------------------------- 1 | import {module, test} from 'qunit'; 2 | import { Model, Factory, Server } from '@bigtest/mirage'; 3 | 4 | module('Integration | Server with ORM', { 5 | beforeEach() { 6 | this.server = new Server({ 7 | environment: 'test', 8 | models: { 9 | blogPost: Model 10 | }, 11 | factories: { 12 | blogPost: Factory 13 | } 14 | }); 15 | this.server.timing = 0; 16 | this.server.logging = false; 17 | }, 18 | afterEach() { 19 | this.server.shutdown(); 20 | } 21 | }); 22 | 23 | test('a single blogPost db collection is made', function(assert) { 24 | assert.equal(this.server.db._collections.length, 1); 25 | assert.equal(this.server.db._collections[0].name, 'blogPosts'); 26 | }); 27 | 28 | test('create looks up the appropriate db collection', function(assert) { 29 | this.server.create('blog-post'); 30 | 31 | assert.equal(this.server.db.blogPosts.length, 1); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/jquery-shim.js: -------------------------------------------------------------------------------- 1 | /* eslint no-undef: 0 */ 2 | 3 | if (typeof jQuery !== 'function') { 4 | throw new Error('cannot find jQuery. This is a problem.'); 5 | } 6 | 7 | export default jQuery; 8 | -------------------------------------------------------------------------------- /tests/qunit-shim.js: -------------------------------------------------------------------------------- 1 | /* eslint no-undef: 0 */ 2 | 3 | if (!QUnit) { 4 | throw new Error('Uh, no. Come back after you have taken the `qunitjs` file, put it in a script tag and put that into a DOM somewhere'); 5 | } 6 | 7 | export const test = QUnit.test; 8 | export const assert = QUnit.assert; 9 | export const skip = QUnit.skip; 10 | 11 | // because hey, the transpiled code declares `module` 12 | const _module = QUnit.module; 13 | export { _module as module }; 14 | -------------------------------------------------------------------------------- /tests/unit/faker-test.js: -------------------------------------------------------------------------------- 1 | import { faker } from '@bigtest/mirage'; 2 | 3 | import {module, test} from 'qunit'; 4 | 5 | module('Unit | Faker'); 6 | 7 | test('#cycle - returns a function', function(assert) { 8 | let callback = faker.list.cycle('first', 'second'); 9 | assert.ok(typeof callback === 'function', 'result is a function'); 10 | }); 11 | 12 | test('#cycle - cycles the passed data', function(assert) { 13 | let callback = faker.list.cycle('first', 'second', 'third'); 14 | 15 | assert.equal(callback(0), 'first', 'return the first result for sequence 0'); 16 | assert.equal(callback(1), 'second', 'return the first result for sequence 1'); 17 | assert.equal(callback(2), 'third', 'return the first result for sequence 2'); 18 | assert.equal(callback(3), 'first', 'return the first result for sequence 3'); 19 | }); 20 | 21 | test('#random - returns random element from a list', function(assert) { 22 | let callback = faker.list.random('first', 'second', 'third'); 23 | 24 | assert.notEqual(['first', 'second', 'third'].indexOf(callback()), -1, 'returns random value'); 25 | }); 26 | 27 | test('#range - creates a random number in a range', function(assert) { 28 | let min = 0; 29 | let max = 10; 30 | 31 | let callback = faker.random.number.range(min, max); 32 | assert.equal(callback() >= min, true, 'result is higher or equal than low value'); 33 | assert.equal(callback() <= max, true, 'result is lower or equal than high value'); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/unit/inflector-test.js: -------------------------------------------------------------------------------- 1 | import { singularize, pluralize, dasherize, camelize } from '@bigtest/mirage'; 2 | 3 | import {module, test} from 'qunit'; 4 | 5 | module('Unit | Inflector'); 6 | 7 | test('can singularize', function(assert) { 8 | assert.equal(singularize('tests'), 'test'); 9 | assert.equal(singularize('watches'), 'watch'); 10 | assert.equal(singularize('sheep'), 'sheep'); 11 | }); 12 | 13 | test('can pluralize', function(assert) { 14 | assert.equal(pluralize('test'), 'tests'); 15 | assert.equal(pluralize('watch'), 'watches'); 16 | assert.equal(pluralize('sheep'), 'sheep'); 17 | }); 18 | 19 | test('camelize does not capitalize the first letter', function(assert) { 20 | assert.equal(camelize('the_big_lebowski'), 'theBigLebowski'); 21 | }); 22 | 23 | test('can convert from kebab-case to camel case', function(assert) { 24 | assert.equal(camelize('the-big-lebowski'), 'theBigLebowski'); 25 | }); 26 | 27 | test('can convert from camel case to kebab case', function(assert) { 28 | assert.equal(dasherize('theBigLebowski'), 'the-big-lebowski'); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/unit/model-test.js: -------------------------------------------------------------------------------- 1 | import { Model } from '@bigtest/mirage'; 2 | import { module, test } from 'qunit'; 3 | 4 | module('Unit | Model'); 5 | 6 | test('it can be instantiated', function(assert) { 7 | let model = new Model({}, 'user'); 8 | assert.ok(model); 9 | }); 10 | 11 | test('it cannot be instantiated without a schema', function(assert) { 12 | assert.throws(function() { 13 | new Model(); 14 | }, /requires a schema/); 15 | }); 16 | 17 | test('it cannot be instantiated without a modelName', function(assert) { 18 | assert.throws(function() { 19 | new Model({}); 20 | }, /requires a modelName/); 21 | }); 22 | 23 | test('findBelongsToAssociation returns association for given type if defined', function(assert) { 24 | let ModelClass = Model.extend(); 25 | let authorAssociationMock = {}; 26 | ModelClass.prototype.belongsToAssociations = { 27 | author: authorAssociationMock 28 | }; 29 | 30 | assert.equal(ModelClass.findBelongsToAssociation('article'), null); 31 | assert.deepEqual(ModelClass.findBelongsToAssociation('author'), authorAssociationMock); 32 | }); 33 | -------------------------------------------------------------------------------- /tests/unit/reference-sort-test.js: -------------------------------------------------------------------------------- 1 | import { referenceSort } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | module('mirage:reference-sort'); 5 | 6 | test('it sorts property references', function(assert) { 7 | let sorted = referenceSort([ 8 | ['propA'], 9 | ['propB', 'propC'], 10 | ['propC', 'propA'], 11 | ['propD'] 12 | ]); 13 | 14 | assert.deepEqual(sorted, ['propD', 'propA', 'propC', 'propB']); 15 | }); 16 | 17 | test('it throws on circular dependency', function(assert) { 18 | assert.throws(function() { 19 | referenceSort([ 20 | ['propA', 'propB'], 21 | ['propB', 'propA'] 22 | ]); 23 | }, function(e) { 24 | return e.toString() === 'Error: Cyclic dependency in properties ["propB","propA"]'; 25 | }); 26 | }); 27 | 28 | test('it works with no references', function(assert) { 29 | let sorted = referenceSort([ 30 | ['propA'], 31 | ['propB'], 32 | ['propC'], 33 | ['propD'] 34 | ]); 35 | 36 | assert.deepEqual(sorted, ['propD', 'propC', 'propB', 'propA']); 37 | }); 38 | -------------------------------------------------------------------------------- /tests/unit/response-test.js: -------------------------------------------------------------------------------- 1 | import { Response } from '@bigtest/mirage'; 2 | 3 | import {module, test} from 'qunit'; 4 | 5 | module('Unit | Response'); 6 | 7 | test('it can be instantiated and return a rack response', function(assert) { 8 | let response = new Response(404, {}, {}); 9 | 10 | assert.ok(response); 11 | assert.ok(response.toRackResponse()); 12 | }); 13 | 14 | test('it can be instantiated with just a response code', function(assert) { 15 | let response = new Response(404); 16 | 17 | assert.ok(response); 18 | assert.ok(response.toRackResponse()); 19 | }); 20 | -------------------------------------------------------------------------------- /tests/unit/schema-test.js: -------------------------------------------------------------------------------- 1 | import { Schema, Db, Model, belongsTo } from '@bigtest/mirage'; 2 | import {module, test} from 'qunit'; 3 | 4 | module('Unit | Schema'); 5 | 6 | test('it can be instantiated', function(assert) { 7 | let dbMock = {}; 8 | let schema = new Schema(dbMock); 9 | assert.ok(schema); 10 | }); 11 | 12 | test('it cannot be instantiated without a db', function(assert) { 13 | assert.throws(function() { 14 | new Schema(); 15 | }, /requires a db/); 16 | }); 17 | 18 | test('modelFor returns model for given type if registered', function(assert) { 19 | let db = new Db(); 20 | let schema = new Schema(db); 21 | 22 | assert.equal(schema.modelFor('article'), null); 23 | 24 | let authorModel = Model.extend({ 25 | }); 26 | let articleModel = Model.extend({ 27 | author: belongsTo() 28 | }); 29 | schema.registerModel('article', articleModel); 30 | schema.registerModel('author', authorModel); 31 | 32 | assert.deepEqual(schema.modelFor('article').foreignKeys, ['authorId']); 33 | assert.deepEqual(schema.modelFor('author').foreignKeys, []); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/unit/serializers/active-model-serializer-test.js: -------------------------------------------------------------------------------- 1 | import { ActiveModelSerializer } from '@bigtest/mirage'; 2 | 3 | import {module, test} from 'qunit'; 4 | 5 | module('Unit | Serializers | ActiveModelSerializer', { 6 | beforeEach() { 7 | this.serializer = new ActiveModelSerializer(); 8 | } 9 | }); 10 | 11 | test('normalize works', function(assert) { 12 | let payload = { 13 | contact: { 14 | id: 1, 15 | name: 'Link' 16 | } 17 | }; 18 | let jsonApiDoc = this.serializer.normalize(payload); 19 | 20 | assert.deepEqual(jsonApiDoc, { 21 | data: { 22 | type: 'contacts', 23 | id: 1, 24 | attributes: { 25 | name: 'Link' 26 | } 27 | } 28 | }); 29 | }); 30 | 31 | test('it hyphenates snake_cased words', function(assert) { 32 | let payload = { 33 | contact: { 34 | id: 1, 35 | first_name: 'Link' 36 | } 37 | }; 38 | let jsonApiDoc = this.serializer.normalize(payload); 39 | 40 | assert.deepEqual(jsonApiDoc, { 41 | data: { 42 | type: 'contacts', 43 | id: 1, 44 | attributes: { 45 | 'first-name': 'Link' 46 | } 47 | } 48 | }); 49 | }); 50 | 51 | test('it works without an id', function(assert) { 52 | let payload = { 53 | contact: { 54 | first_name: 'Link', 55 | last_name: 'zor' 56 | } 57 | }; 58 | let jsonApiDoc = this.serializer.normalize(payload); 59 | 60 | assert.deepEqual(jsonApiDoc, { 61 | data: { 62 | type: 'contacts', 63 | attributes: { 64 | 'first-name': 'Link', 65 | 'last-name': 'zor' 66 | } 67 | } 68 | }); 69 | }); 70 | -------------------------------------------------------------------------------- /tests/unit/serializers/rest-serializer-test.js: -------------------------------------------------------------------------------- 1 | import { RestSerializer } from '@bigtest/mirage'; 2 | 3 | import {module, test} from 'qunit'; 4 | 5 | module('Unit | Serializers | RestSerializer', { 6 | beforeEach() { 7 | this.serializer = new RestSerializer(); 8 | } 9 | }); 10 | 11 | test('it hyphenates camelized words', function(assert) { 12 | let payload = { 13 | 'person': { 14 | 'id': 1, 15 | 'firstName': 'Rick', 16 | 'lastName': 'Sanchez' 17 | } 18 | }; 19 | let jsonApiDoc = this.serializer.normalize(payload); 20 | 21 | assert.deepEqual(jsonApiDoc, { 22 | data: { 23 | type: 'people', 24 | id: 1, 25 | attributes: { 26 | 'first-name': 'Rick', 27 | 'last-name': 'Sanchez' 28 | } 29 | } 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /tests/unit/utils/normalize-name-test.js: -------------------------------------------------------------------------------- 1 | import { toCollectionName, toModelName } from '@bigtest/mirage'; 2 | import { inflections } from 'inflected'; 3 | 4 | import {module, test} from 'qunit'; 5 | 6 | const EN = inflections('en'); 7 | 8 | module('Unit | Normalize name'); 9 | 10 | test('can convert Model name to DbCollection name', function(assert) { 11 | assert.equal(toCollectionName('test'), 'tests'); 12 | assert.equal(toCollectionName('hard-test'), 'hardTests'); 13 | }); 14 | 15 | test('can convert DbCollection name to Model name', function(assert) { 16 | assert.equal(toModelName('tests'), 'test'); 17 | assert.equal(toModelName('hardTests'), 'hard-test'); 18 | }); 19 | 20 | test('can convert Model name to DbCollection using custom inflector rules', function(assert) { 21 | EN.irregular('head-of-state', 'heads-of-state'); 22 | assert.equal(toCollectionName('head-of-state'), 'headsOfState'); 23 | }); 24 | 25 | test('can convert DbCollection name to Model name using custom inflector rules', function(assert) { 26 | EN.irregular('head-of-state', 'heads-of-state'); 27 | assert.equal(toModelName('headsOfState'), 'head-of-state'); 28 | }); 29 | --------------------------------------------------------------------------------