Skip to content

Commit

Permalink
saveRecord tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Apr 5, 2024
1 parent e3b3bdf commit 4c78037
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/legacy-compat/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 0 additions & 2 deletions packages/legacy-compat/src/builders/save-record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ export function saveRecordBuilder<T>(record: T, options: Record<string, unknown>
/*
TODO:
* [] test this
* [] make sure nothing fails bc of willCommit change
* [] cargo cult jsdoc setup from json-api/src/-private/builders/query.ts
*/
292 changes: 292 additions & 0 deletions tests/main/tests/integration/legacy-compat/save-record-test.ts
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`
);
});
});
});

0 comments on commit 4c78037

Please sign in to comment.