Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions tests/main/tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2653,4 +2653,224 @@ module('integration/unload - Unloading Records', function (hooks) {
assert.notStrictEqual(store.peekRecord('person', '1'), null, 'record is still in the store');
assert.true(person.isDestroyed, 'original record is destroyed');
});

test('edit then unloadAll removes all records (async) (emberjs/data#8863)', async function (assert) {
class Company extends Model {
@attr name;
}

this.owner.register('model:company', Company);
this.owner.register(
'adapter:company',
class {
updateRecord() {
return Promise.resolve({
data: {
id: '1',
type: 'company',
attributes: {
name: 'Rebrand',
},
},
});
}

static create() {
return new this();
}
}
);
const store = this.owner.lookup('service:store');

const editRecord = store.push({
data: {
id: '1',
type: 'company',
attributes: {
name: 'ACME',
},
},

included: [
{
id: '2',
type: 'company',
attributes: {
name: 'EMCA',
},
},
],
});

assert.strictEqual(store.peekAll('company').length, 2, '2 companies loaded');
editRecord.name = 'Rebrand';
await editRecord.save();
assert.false(editRecord.hasDirtyAttributes, 'edit record does not have dirty attrs after save');

store.unloadAll('company');
await settled();

assert.strictEqual(store.peekAll('company').length, 0, 'peekAll 0 - companies unloaded');
});

test('edit then unloadAll removes all records (sync) (emberjs/data#8863)', async function (assert) {
class Company extends Model {
@attr name;
}

this.owner.register('model:company', Company);
this.owner.register(
'adapter:company',
class {
updateRecord() {
return Promise.resolve({
data: {
id: '1',
type: 'company',
attributes: {
name: 'Rebrand',
},
},
});
}

static create() {
return new this();
}
}
);
const store = this.owner.lookup('service:store');

const editRecord = store.push({
data: {
id: '1',
type: 'company',
attributes: {
name: 'ACME',
},
},

included: [
{
id: '2',
type: 'company',
attributes: {
name: 'EMCA',
},
},
],
});

assert.strictEqual(store.peekAll('company').length, 2, '2 companies loaded');
editRecord.name = 'Rebrand';
assert.true(editRecord.hasDirtyAttributes, 'edit record has dirty attrs before save');
await editRecord.save();
assert.false(editRecord.hasDirtyAttributes, 'edit record does not have dirty attrs after save');

store.unloadAll('company');

assert.strictEqual(store.peekAll('company').length, 0, 'peekAll 0 - companies unloaded');
});

test('edit then unloadAll removes all records (async) - no save response (emberjs/data#8863)', async function (assert) {
class Company extends Model {
@attr name;
}

this.owner.register('model:company', Company);
this.owner.register(
'adapter:company',
class {
updateRecord() {
return Promise.resolve();
}

static create() {
return new this();
}
}
);
const store = this.owner.lookup('service:store');

const editRecord = store.push({
data: {
id: '1',
type: 'company',
attributes: {
name: 'ACME',
},
},

included: [
{
id: '2',
type: 'company',
attributes: {
name: 'EMCA',
},
},
],
});

assert.strictEqual(store.peekAll('company').length, 2, '2 companies loaded');
editRecord.name = 'Rebrand';
await editRecord.save();
assert.false(editRecord.hasDirtyAttributes, 'edit record does not have dirty attrs after save');

store.unloadAll('company');
await settled();

assert.strictEqual(store.peekAll('company').length, 0, 'peekAll 0 - companies unloaded');
});

test('edit then unloadAll removes all records (sync) - no save response (emberjs/data#8863)', async function (assert) {
class Company extends Model {
@attr name;
}

this.owner.register('model:company', Company);
this.owner.register(
'adapter:company',
class {
updateRecord() {
return Promise.resolve();
}

static create() {
return new this();
}
}
);
const store = this.owner.lookup('service:store');

const editRecord = store.push({
data: {
id: '1',
type: 'company',
attributes: {
name: 'ACME',
},
},

included: [
{
id: '2',
type: 'company',
attributes: {
name: 'EMCA',
},
},
],
});

assert.strictEqual(store.peekAll('company').length, 2, '2 companies loaded');
editRecord.name = 'Rebrand';
assert.true(editRecord.hasDirtyAttributes, 'edit record has dirty attrs before save');
await editRecord.save();
assert.false(editRecord.hasDirtyAttributes, 'edit record does not have dirty attrs after save');

store.unloadAll('company');

assert.strictEqual(store.peekAll('company').length, 0, 'peekAll 0 - companies unloaded');
});
});