Skip to content

Commit 5bd87fe

Browse files
committed
Add other tests, comment out saveRecord asserts
1 parent 9d8be6c commit 5bd87fe

File tree

5 files changed

+356
-6
lines changed

5 files changed

+356
-6
lines changed

packages/active-record/src/-private/builders/save-record.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions
139139
export function createRecord(record: unknown, options: ConstrainedRequestOptions = {}): CreateRequestOptions {
140140
const identifier = recordIdentifierFor(record);
141141
assert(`Expected to be given a record instance`, identifier);
142-
assert(`Cannot delete a record that does not have an associated type and id.`, isExisting(identifier));
142+
// assert(`Cannot delete a record that does not have an associated type and id.`, isExisting(identifier));
143143

144144
const urlOptions: CreateRecordUrlOptions = {
145145
identifier: identifier,

packages/rest/src/-private/builders/save-record.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export function deleteRecord(record: unknown, options: ConstrainedRequestOptions
139139
export function createRecord(record: unknown, options: ConstrainedRequestOptions = {}): CreateRequestOptions {
140140
const identifier = recordIdentifierFor(record);
141141
assert(`Expected to be given a record instance`, identifier);
142-
assert(`Cannot delete a record that does not have an associated type and id.`, isExisting(identifier));
142+
// assert(`Cannot delete a record that does not have an associated type and id.`, isExisting(identifier));
143143

144144
const urlOptions: CreateRecordUrlOptions = {
145145
identifier: identifier,

tests/builders/tests/unit/active-record-builder-test.ts

+164-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { module, test } from 'qunit';
22

3-
import { findRecord, query } from '@ember-data/active-record/request';
3+
import { setupTest } from 'ember-qunit';
4+
5+
import { createRecord, deleteRecord, findRecord, query, updateRecord } from '@ember-data/active-record/request';
46
import { setBuildURLConfig } from '@ember-data/request-utils';
7+
import Store, { recordIdentifierFor } from '@ember-data/store';
58

9+
import UserSetting from '../../app/models/user-setting';
610
import { headersToObject } from '../helpers/utils';
711

812
const ACTIVE_RECORD_HEADERS = { 'content-type': 'application/json; charset=utf-8' };
913

1014
module('ActiveRecord | Request Builders', function (hooks) {
15+
setupTest(hooks);
16+
1117
hooks.beforeEach(function () {
1218
setBuildURLConfig({ host: 'https://api.example.com', namespace: 'api/v1' });
1319
});
@@ -109,4 +115,161 @@ module('ActiveRecord | Request Builders', function (hooks) {
109115
);
110116
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS);
111117
});
118+
119+
test('createRecord passing store record', function (assert) {
120+
const store = this.owner.lookup('service:store') as Store;
121+
const userSetting = store.createRecord('user-setting', {
122+
name: 'test',
123+
});
124+
const identifier = recordIdentifierFor(userSetting);
125+
const result = createRecord(userSetting);
126+
127+
assert.deepEqual(
128+
result,
129+
{
130+
url: 'https://api.example.com/api/v1/user_settings',
131+
method: 'POST',
132+
headers: new Headers(ACTIVE_RECORD_HEADERS),
133+
op: 'createRecord',
134+
data: {
135+
record: identifier,
136+
},
137+
},
138+
`createRecord works with record identifier passed`
139+
);
140+
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS, "headers are set to ActiveRecord API's");
141+
});
142+
143+
test('createRecord passing store record and options', function (assert) {
144+
const store = this.owner.lookup('service:store') as Store;
145+
const userSetting = store.createRecord('user-setting', {
146+
name: 'test',
147+
});
148+
const identifier = recordIdentifierFor(userSetting);
149+
const result = createRecord(userSetting, { resourcePath: 'user-settings/new' });
150+
151+
assert.deepEqual(
152+
result,
153+
{
154+
url: 'https://api.example.com/api/v1/user-settings/new',
155+
method: 'POST',
156+
headers: new Headers(ACTIVE_RECORD_HEADERS),
157+
op: 'createRecord',
158+
data: {
159+
record: identifier,
160+
},
161+
},
162+
`createRecord works with record identifier passed`
163+
);
164+
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS, "headers are set to ActiveRecord API's");
165+
});
166+
167+
test('updateRecord passing store record', function (assert) {
168+
const store = this.owner.lookup('service:store') as Store;
169+
170+
const expectedData = {
171+
data: {
172+
id: '12',
173+
type: 'user-setting',
174+
attributes: {
175+
name: 'test',
176+
},
177+
},
178+
};
179+
store.push(expectedData);
180+
181+
const userSetting = store.peekRecord('user-setting', '12') as UserSetting;
182+
const identifier = recordIdentifierFor(userSetting);
183+
184+
userSetting.name = 'test2';
185+
186+
const result = updateRecord(userSetting);
187+
188+
assert.deepEqual(
189+
result,
190+
{
191+
url: 'https://api.example.com/api/v1/user_settings/12',
192+
method: 'PUT',
193+
headers: new Headers(ACTIVE_RECORD_HEADERS),
194+
op: 'updateRecord',
195+
data: {
196+
record: identifier,
197+
},
198+
},
199+
`updateRecord works with record identifier passed`
200+
);
201+
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS, "headers are set to ActiveRecord API's");
202+
});
203+
204+
test('updateRecord with PATCH method', function (assert) {
205+
const store = this.owner.lookup('service:store') as Store;
206+
207+
const expectedData = {
208+
data: {
209+
id: '12',
210+
type: 'user-setting',
211+
attributes: {
212+
name: 'test',
213+
},
214+
},
215+
};
216+
store.push(expectedData);
217+
218+
const userSetting = store.peekRecord('user-setting', '12') as UserSetting;
219+
const identifier = recordIdentifierFor(userSetting);
220+
221+
userSetting.name = 'test2';
222+
223+
const result = updateRecord(userSetting, { patch: true });
224+
225+
assert.deepEqual(
226+
result,
227+
{
228+
url: 'https://api.example.com/api/v1/user_settings/12',
229+
method: 'PATCH',
230+
headers: new Headers(ACTIVE_RECORD_HEADERS),
231+
op: 'updateRecord',
232+
data: {
233+
record: identifier,
234+
},
235+
},
236+
`updateRecord works with patch option`
237+
);
238+
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS, "headers are set to ActiveRecord API's");
239+
});
240+
241+
test('deleteRecord with identifier', function (assert) {
242+
const store = this.owner.lookup('service:store') as Store;
243+
244+
const expectedData = {
245+
data: {
246+
id: '12',
247+
type: 'user-setting',
248+
attributes: {
249+
name: 'test',
250+
},
251+
},
252+
};
253+
store.push(expectedData);
254+
255+
const userSetting = store.peekRecord('user-setting', '12');
256+
const identifier = recordIdentifierFor(userSetting);
257+
258+
const result = deleteRecord(userSetting);
259+
260+
assert.deepEqual(
261+
result,
262+
{
263+
url: 'https://api.example.com/api/v1/user_settings/12',
264+
method: 'DELETE',
265+
headers: new Headers(ACTIVE_RECORD_HEADERS),
266+
op: 'deleteRecord',
267+
data: {
268+
record: identifier,
269+
},
270+
},
271+
`deleteRecord works with patch option`
272+
);
273+
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS, "headers are set to ActiveRecord API's");
274+
});
112275
});

tests/builders/tests/unit/json-api-builder-test.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ module('JSON:API | Request Builders', function (hooks) {
116116
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
117117
});
118118

119-
test('createRecord with store.createRecord', function (assert) {
119+
test('createRecord passing store record', function (assert) {
120120
const store = this.owner.lookup('service:store') as Store;
121121
const userSetting = store.createRecord('user-setting', {
122122
name: 'test',
@@ -140,7 +140,31 @@ module('JSON:API | Request Builders', function (hooks) {
140140
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS, "headers are set to JSON API's");
141141
});
142142

143-
test('updateRecord with identifier', function (assert) {
143+
test('createRecord passing store record and options', function (assert) {
144+
const store = this.owner.lookup('service:store') as Store;
145+
const userSetting = store.createRecord('user-setting', {
146+
name: 'test',
147+
});
148+
const identifier = recordIdentifierFor(userSetting);
149+
const result = createRecord(userSetting, { resourcePath: 'user-settings/new' });
150+
151+
assert.deepEqual(
152+
result,
153+
{
154+
url: 'https://api.example.com/api/v1/user-settings/new',
155+
method: 'POST',
156+
headers: new Headers(JSON_API_HEADERS),
157+
op: 'createRecord',
158+
data: {
159+
record: identifier,
160+
},
161+
},
162+
`createRecord works with record identifier passed`
163+
);
164+
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS, "headers are set to JSON API's");
165+
});
166+
167+
test('updateRecord passing store record', function (assert) {
144168
const store = this.owner.lookup('service:store') as Store;
145169

146170
const expectedData = {

0 commit comments

Comments
 (0)