From 4c78037d16cd8a656047d60e25cdff20856c00e4 Mon Sep 17 00:00:00 2001 From: Krystan HuffMenne Date: Fri, 5 Apr 2024 11:09:35 -0700 Subject: [PATCH] saveRecord tests --- packages/legacy-compat/src/builders.ts | 3 + .../legacy-compat/src/builders/save-record.ts | 2 - .../legacy-compat/save-record-test.ts | 292 ++++++++++++++++++ 3 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 tests/main/tests/integration/legacy-compat/save-record-test.ts diff --git a/packages/legacy-compat/src/builders.ts b/packages/legacy-compat/src/builders.ts index bac3f3c5a7a..d52e599c292 100644 --- a/packages/legacy-compat/src/builders.ts +++ b/packages/legacy-compat/src/builders.ts @@ -6,3 +6,6 @@ export type { FindRecordBuilderOptions, FindRecordRequestInput } from './builder export { queryBuilder as query, queryRecordBuilder as queryRecord } from './builders/query'; export type { QueryBuilderOptions, QueryRecordRequestInput, QueryRequestInput } from './builders/query'; + +export { saveRecordBuilder as saveRecord } from './builders/save-record'; +export type { SaveRecordBuilderOptions, SaveRecordRequestInput } from './builders/save-record'; diff --git a/packages/legacy-compat/src/builders/save-record.ts b/packages/legacy-compat/src/builders/save-record.ts index 74fdbc544ae..b9ba3d345c9 100644 --- a/packages/legacy-compat/src/builders/save-record.ts +++ b/packages/legacy-compat/src/builders/save-record.ts @@ -85,8 +85,6 @@ export function saveRecordBuilder(record: T, options: Record /* TODO: -* [] test this -* [] make sure nothing fails bc of willCommit change * [] cargo cult jsdoc setup from json-api/src/-private/builders/query.ts */ diff --git a/tests/main/tests/integration/legacy-compat/save-record-test.ts b/tests/main/tests/integration/legacy-compat/save-record-test.ts new file mode 100644 index 00000000000..44311c02c80 --- /dev/null +++ b/tests/main/tests/integration/legacy-compat/save-record-test.ts @@ -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(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 = { + 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(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 = { + 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(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 = { + 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` + ); + }); + }); +});