Skip to content

Instantly share code, notes, and snippets.

@bastimeyer
Created February 15, 2018 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastimeyer/ecb84112ba24082dfa58fdd3596ef978 to your computer and use it in GitHub Desktop.
Save bastimeyer/ecb84112ba24082dfa58fdd3596ef978 to your computer and use it in GitHub Desktop.
destroyRecord and unloadRecord in EmberData 3.0.1 (broken since 2.12)
7. one is unregistered
Expected: false
Result: true
8. one doesn't exist
Expected: false
Result: true
11. two doesn't exist
Expected: false
Result: true
12. Expecting identityMap to be empty, since all records have been unloaded
Expected: []
Result: [
[
"one",
"root.deleted.saved"
],
[
"two",
"root.empty"
]
]
13. Assertion Failed: The id one has already been used with another record for modelClass 'foo'
Expected: true
Result: false
import { moduleForModel, test } from "ember-qunit";
import { run } from "@ember/runloop";
moduleForModel( "foo", "Unit | Model | foo", {
needs: []
});
test( "destroy or unload record", async function( assert ) {
const store = this.store();
const [ one, two ] = run( () => [
store.createRecord( "foo", { id: "one" } ),
store.createRecord( "foo", { id: "two" } )
]);
const all = store.peekAll( "foo" );
// all good!
assert.propEqual( all.mapBy( "id" ), [ "one", "two" ], "peekAll includes all records" );
assert.ok( store.hasRecordForId( "foo", "one" ), "one is registered" );
assert.ok( store.hasRecordForId( "foo", "two" ), "two is registered" );
assert.ok( !!store.recordForId( "foo", "one" ), "one exists" );
assert.ok( !!store.recordForId( "foo", "two" ), "two exists" );
// destroy and unload record one
await run( () => one.destroyRecord() );
assert.propEqual( all.mapBy( "id" ), [ "two" ], "peekAll only includes record two" );
// fails
assert.notOk( store.hasRecordForId( "foo", "one" ), "one is unregistered" );
// fails
assert.notOk( !!store.recordForId( "foo", "one" ), "one doesn't exist" );
// just unload record two
run( () => two.unloadRecord() );
assert.propEqual( all.mapBy( "id" ), [], "peekAll is empty" );
// passes
assert.notOk( store.hasRecordForId( "foo", "two" ), "two is unregistered" );
// fails... wtf?
assert.notOk( !!store.recordForId( "foo", "two" ), "two doesn't exist" );
// not sure why ED does this, but why are the internal models still being kept?
assert.propEqual(
store._identityMap._map.foo.models.map( model => [ model.id, model.currentState.stateName ]),
[],
"Expecting identityMap to be empty, since all records have been unloaded"
);
// failure and a big issue: can't re-use previous model IDs
run( () => [
store.createRecord( "foo", { id: "one" } ),
store.createRecord( "foo", { id: "two" } )
]);
});
import DS from 'ember-data';
export default DS.Model.extend({});
@sly7-7
Copy link

sly7-7 commented Feb 16, 2018

For:

 // fails... wtf?
	assert.notOk( !!store.recordForId( "foo", "two" ), "two doesn't exist" );

  // not sure why ED does this, but why are the internal models still being kept?
	assert.propEqual(
		store._identityMap._map.foo.models.map( model => [ model.id, model.currentState.stateName ]),
		[],
		"Expecting identityMap to be empty, since all records have been unloaded"
	);

I think ED is keeping internals on purpose, and record is 'empty'. If you reload, I think this improves the load time.

@bastimeyer
Copy link
Author

I think ED is keeping internals on purpose
If you reload, I think this improves the load time.

Doesn't make sense for locally created records, though. Especially if you're trying to create a new record with the same ID later on, like in the failing assertion at the end of the test. It's also quite an unexpected behavior for recordForId to return something while hasRecordForId returns false...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment