-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3b3bdf
commit 4c78037
Showing
3 changed files
with
295 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
292 changes: 292 additions & 0 deletions
292
tests/main/tests/integration/legacy-compat/save-record-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
import { module, test } from 'qunit'; | ||
|
||
import { setupTest } from 'ember-qunit'; | ||
|
||
import type { CompatStore } from '@ember-data/legacy-compat'; | ||
import type { SaveRecordBuilderOptions } from '@ember-data/legacy-compat/builders'; | ||
import { saveRecord } from '@ember-data/legacy-compat/builders'; | ||
import Model, { attr } from '@ember-data/model'; | ||
import { recordIdentifierFor } from '@ember-data/store'; | ||
|
||
class Post extends Model { | ||
@attr declare name: string; | ||
} | ||
|
||
module('Integration - legacy-compat/builders/saveRecord', function (hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function () { | ||
this.owner.register('model:post', Post); | ||
}); | ||
|
||
module('createRecord', function () { | ||
test('basic payload', async function (assert) { | ||
this.owner.register( | ||
'adapter:application', | ||
class Adapter { | ||
createRecord() { | ||
assert.step('adapter-createRecord'); | ||
return Promise.resolve({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}); | ||
} | ||
static create() { | ||
return new this(); | ||
} | ||
} | ||
); | ||
|
||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const newPost = store.createRecord('post', { name: 'Krystan rules, you drool' }); | ||
const { content: savedPost } = await store.request<Post>(saveRecord(newPost)); | ||
|
||
assert.strictEqual(savedPost.id, '1', 'post has correct id'); | ||
assert.strictEqual(savedPost.name, 'Krystan rules, you drool', 'post has correct name'); | ||
assert.verifySteps(['adapter-createRecord'], 'adapter-createRecord was called'); | ||
}); | ||
|
||
test('saveRecord', function (assert) { | ||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const newPost = store.createRecord('post', { name: 'Krystan rules, you drool' }); | ||
const identifier = recordIdentifierFor(newPost); | ||
const result = saveRecord(newPost); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
op: 'createRecord', | ||
data: { | ||
record: identifier, | ||
options: {}, | ||
}, | ||
records: [identifier], | ||
cacheOptions: {}, | ||
}, | ||
`saveRecord works` | ||
); | ||
}); | ||
|
||
test('saveRecord with options', function (assert) { | ||
const options: Required<SaveRecordBuilderOptions> = { | ||
whatever: true, | ||
adapterOptions: {}, | ||
}; | ||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const newPost = store.createRecord('post', { name: 'Krystan rules, you drool' }); | ||
const identifier = recordIdentifierFor(newPost); | ||
const result = saveRecord(newPost, options); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
op: 'createRecord', | ||
data: { | ||
record: identifier, | ||
options: options, | ||
}, | ||
records: [identifier], | ||
cacheOptions: {}, | ||
}, | ||
`saveRecord works` | ||
); | ||
}); | ||
}); | ||
|
||
module('deleteRecord', function () { | ||
test('basic payload', async function (assert) { | ||
this.owner.register( | ||
'adapter:application', | ||
class Adapter { | ||
deleteRecord() { | ||
assert.step('adapter-deleteRecord'); | ||
return Promise.resolve(); | ||
} | ||
static create() { | ||
return new this(); | ||
} | ||
} | ||
); | ||
|
||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const existingPost = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}) as Post; | ||
existingPost.deleteRecord(); | ||
const { content: savedPost } = await store.request<Post>(saveRecord(existingPost)); | ||
|
||
assert.strictEqual(savedPost.id, '1', 'post has correct id'); | ||
assert.strictEqual(savedPost.name, 'Krystan rules, you drool', 'post has correct name'); | ||
assert.true(savedPost.isDeleted, 'post isDeleted'); | ||
assert.verifySteps(['adapter-deleteRecord'], 'adapter-deleteRecord was called'); | ||
}); | ||
|
||
test('saveRecord', function (assert) { | ||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const existingPost = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}) as Post; | ||
existingPost.deleteRecord(); | ||
const identifier = recordIdentifierFor(existingPost); | ||
const result = saveRecord(existingPost); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
op: 'deleteRecord', | ||
data: { | ||
record: identifier, | ||
options: {}, | ||
}, | ||
records: [identifier], | ||
cacheOptions: {}, | ||
}, | ||
`saveRecord works` | ||
); | ||
}); | ||
|
||
test('saveRecord with options', function (assert) { | ||
const options: Required<SaveRecordBuilderOptions> = { | ||
whatever: true, | ||
adapterOptions: {}, | ||
}; | ||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const existingPost = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}) as Post; | ||
existingPost.deleteRecord(); | ||
const identifier = recordIdentifierFor(existingPost); | ||
const result = saveRecord(existingPost, options); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
op: 'deleteRecord', | ||
data: { | ||
record: identifier, | ||
options: options, | ||
}, | ||
records: [identifier], | ||
cacheOptions: {}, | ||
}, | ||
`saveRecord works` | ||
); | ||
}); | ||
}); | ||
|
||
module('updateRecord', function () { | ||
test('basic payload', async function (assert) { | ||
this.owner.register( | ||
'adapter:application', | ||
class Adapter { | ||
updateRecord() { | ||
assert.step('adapter-updateRecord'); | ||
return Promise.resolve(); | ||
} | ||
static create() { | ||
return new this(); | ||
} | ||
} | ||
); | ||
|
||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const existingPost = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}) as Post; | ||
existingPost.name = 'Chris drools, Krystan rules'; | ||
const { content: savedPost } = await store.request<Post>(saveRecord(existingPost)); | ||
|
||
assert.strictEqual(savedPost.id, '1', 'post has correct id'); | ||
assert.strictEqual(savedPost.name, 'Krystan rules, you drool', 'post has correct name'); | ||
assert.true(savedPost.isDeleted, 'post isDeleted'); | ||
assert.verifySteps(['adapter-updateRecord'], 'adapter-updateRecord was called'); | ||
}); | ||
|
||
test('saveRecord', function (assert) { | ||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const existingPost = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}) as Post; | ||
existingPost.name = 'Chris drools, Krystan rules'; | ||
const identifier = recordIdentifierFor(existingPost); | ||
const result = saveRecord(existingPost); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
op: 'updateRecord', | ||
data: { | ||
record: identifier, | ||
options: {}, | ||
}, | ||
records: [identifier], | ||
cacheOptions: {}, | ||
}, | ||
`saveRecord works` | ||
); | ||
}); | ||
|
||
test('saveRecord with options', function (assert) { | ||
const options: Required<SaveRecordBuilderOptions> = { | ||
whatever: true, | ||
adapterOptions: {}, | ||
}; | ||
const store = this.owner.lookup('service:store') as CompatStore; | ||
const existingPost = store.push({ | ||
data: { | ||
id: '1', | ||
type: 'post', | ||
attributes: { | ||
name: 'Krystan rules, you drool', | ||
}, | ||
}, | ||
}) as Post; | ||
existingPost.name = 'Chris drools, Krystan rules'; | ||
const identifier = recordIdentifierFor(existingPost); | ||
const result = saveRecord(existingPost, options); | ||
assert.deepEqual( | ||
result, | ||
{ | ||
op: 'updateRecord', | ||
data: { | ||
record: identifier, | ||
options: options, | ||
}, | ||
records: [identifier], | ||
cacheOptions: {}, | ||
}, | ||
`saveRecord works` | ||
); | ||
}); | ||
}); | ||
}); |