Skip to content

Commit b8b3bc3

Browse files
Baltazorerunspired
authored andcommitted
Add working test setup
1 parent 12d4acc commit b8b3bc3

File tree

5 files changed

+125
-37
lines changed

5 files changed

+125
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Model, { attr } from '@ember-data/model';
2+
3+
export default class UserSetting extends Model {
4+
@attr declare name: string;
5+
}

tests/builders/app/services/store.js

-3
This file was deleted.

tests/builders/app/services/store.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import JSONAPICache from '@ember-data/json-api';
2+
import type Model from '@ember-data/model';
3+
import { instantiateRecord, teardownRecord } from '@ember-data/model';
4+
import { buildSchema, modelFor } from '@ember-data/model/hooks';
5+
import RequestManager from '@ember-data/request';
6+
import Fetch from '@ember-data/request/fetch';
7+
import DataStore, { CacheHandler } from '@ember-data/store';
8+
import type { Cache } from '@ember-data/types/cache/cache';
9+
import type { CacheCapabilitiesManager } from '@ember-data/types/q/cache-store-wrapper';
10+
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
11+
12+
export default class Store extends DataStore {
13+
constructor(args: unknown) {
14+
super(args);
15+
16+
const manager = (this.requestManager = new RequestManager());
17+
manager.use([Fetch]);
18+
manager.useCache(CacheHandler);
19+
20+
this.registerSchema(buildSchema(this));
21+
}
22+
23+
createCache(capabilities: CacheCapabilitiesManager): Cache {
24+
return new JSONAPICache(capabilities);
25+
}
26+
27+
instantiateRecord(identifier: StableRecordIdentifier, createRecordArgs: { [key: string]: unknown }): unknown {
28+
return instantiateRecord.call(this, identifier, createRecordArgs);
29+
}
30+
31+
teardownRecord(record: Model): void {
32+
return teardownRecord.call(this, record);
33+
}
34+
35+
modelFor(type: string) {
36+
return modelFor.call(this, type);
37+
}
38+
}

tests/builders/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
"@ember-data/store": {
2929
"injected": true
3030
},
31+
"@ember-data/model": {
32+
"injected": true
33+
},
34+
"@ember-data/legacy-compat": {
35+
"injected": true
36+
},
37+
"@ember-data/request": {
38+
"injected": true
39+
},
3140
"@ember-data/request-utils": {
3241
"injected": true
3342
},
@@ -47,7 +56,10 @@
4756
"@ember-data/active-record": "workspace:5.4.0-alpha.14",
4857
"@ember-data/graph": "workspace:5.4.0-alpha.14",
4958
"@ember-data/json-api": "workspace:5.4.0-alpha.14",
59+
"@ember-data/model": "workspace:5.4.0-alpha.14",
60+
"@ember-data/legacy-compat": "workspace:5.4.0-alpha.14",
5061
"@ember-data/private-build-infra": "workspace:5.4.0-alpha.14",
62+
"@ember-data/request": "workspace:5.4.0-alpha.14",
5163
"@ember-data/request-utils": "workspace:5.4.0-alpha.14",
5264
"@ember-data/rest": "workspace:5.4.0-alpha.14",
5365
"@ember-data/store": "workspace:5.4.0-alpha.14",

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

+70-34
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { setupTest } from 'ember-qunit';
44

55
import { createRecord, deleteRecord, findRecord, query, updateRecord } from '@ember-data/json-api/request';
66
import { setBuildURLConfig } from '@ember-data/request-utils';
7-
import Store from '@ember-data/store';
7+
import Store, { recordIdentifierFor } from '@ember-data/store';
88

99
import { headersToObject } from '../helpers/utils';
1010

@@ -115,14 +115,13 @@ module('JSON:API | Request Builders', function (hooks) {
115115
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
116116
});
117117

118-
test('createRecord with identifier', function (assert) {
118+
test('createRecord with store.createRecord', function (assert) {
119119
const store = this.owner.lookup('service:store') as Store;
120-
const record = { type: 'user-setting' };
121-
const userSettingIdentifier = store.identifierCache.getOrCreateRecordIdentifier(record);
122-
123-
console.log({ userSettingIdentifier });
124-
// TODO: This still fails: `is not a record instantiated by @ember-data/store`
125-
const result = createRecord(userSettingIdentifier);
120+
const userSetting = store.createRecord('user-setting', {
121+
name: 'test',
122+
});
123+
const identifier = recordIdentifierFor(userSetting);
124+
const result = createRecord(userSetting);
126125

127126
assert.deepEqual(
128127
result,
@@ -132,83 +131,120 @@ module('JSON:API | Request Builders', function (hooks) {
132131
headers: new Headers(JSON_API_HEADERS),
133132
op: 'createRecord',
134133
data: {
135-
record,
134+
record: identifier,
136135
},
137136
},
138137
`createRecord works with record identifier passed`
139138
);
140-
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
139+
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS, "headers are set to JSON API's");
141140
});
142141

143-
// Do we need this?
144-
skip('createRecord with store record object', function (assert) {});
145-
146-
skip('updateRecord with identifier', function (assert) {
142+
test('updateRecord with identifier', function (assert) {
147143
const store = this.owner.lookup('service:store') as Store;
148-
const record = { type: 'user-setting' };
149-
const userSettingIdentifier = store.identifierCache.getOrCreateRecordIdentifier(record);
150144

151-
const result = updateRecord(userSettingIdentifier);
145+
const expectedData = {
146+
data: {
147+
id: '12',
148+
type: 'user-setting',
149+
attributes: {
150+
name: 'test',
151+
},
152+
},
153+
};
154+
store.push(expectedData);
155+
156+
const userSetting = store.peekRecord('user-setting', '12');
157+
const identifier = recordIdentifierFor(userSetting);
158+
159+
userSetting.name = 'test2';
160+
161+
const result = updateRecord(userSetting);
152162

153163
assert.deepEqual(
154164
result,
155165
{
156-
url: 'https://api.example.com/api/v1/user-settings',
166+
url: 'https://api.example.com/api/v1/user-settings/12',
157167
method: 'PUT',
158168
headers: new Headers(JSON_API_HEADERS),
159169
op: 'updateRecord',
160170
data: {
161-
record,
171+
record: identifier,
162172
},
163173
},
164174
`updateRecord works with record identifier passed`
165175
);
166-
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
176+
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS, "headers are set to JSON API's");
167177
});
168178

169-
skip('updateRecord with PATCH method', function (assert) {
179+
test('updateRecord with PATCH method', function (assert) {
170180
const store = this.owner.lookup('service:store') as Store;
171-
const record = { type: 'user-setting' };
172-
const userSettingIdentifier = store.identifierCache.getOrCreateRecordIdentifier(record);
173181

174-
const result = updateRecord(userSettingIdentifier, { patch: true });
182+
const expectedData = {
183+
data: {
184+
id: '12',
185+
type: 'user-setting',
186+
attributes: {
187+
name: 'test',
188+
},
189+
},
190+
};
191+
store.push(expectedData);
192+
193+
const userSetting = store.peekRecord('user-setting', '12');
194+
const identifier = recordIdentifierFor(userSetting);
195+
196+
userSetting.name = 'test2';
197+
198+
const result = updateRecord(userSetting, { patch: true });
175199

176200
assert.deepEqual(
177201
result,
178202
{
179-
url: 'https://api.example.com/api/v1/user-settings',
203+
url: 'https://api.example.com/api/v1/user-settings/12',
180204
method: 'PATCH',
181205
headers: new Headers(JSON_API_HEADERS),
182206
op: 'updateRecord',
183207
data: {
184-
record,
208+
record: identifier,
185209
},
186210
},
187211
`updateRecord works with patch option`
188212
);
189-
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
213+
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS, "headers are set to JSON API's");
190214
});
191215

192-
skip('deleteRecord with identifier', function (assert) {
216+
test('deleteRecord with identifier', function (assert) {
193217
const store = this.owner.lookup('service:store') as Store;
194-
const record = { type: 'user-setting' };
195-
const userSettingIdentifier = store.identifierCache.getOrCreateRecordIdentifier(record);
196218

197-
const result = deleteRecord(userSettingIdentifier);
219+
const expectedData = {
220+
data: {
221+
id: '12',
222+
type: 'user-setting',
223+
attributes: {
224+
name: 'test',
225+
},
226+
},
227+
};
228+
store.push(expectedData);
229+
230+
const userSetting = store.peekRecord('user-setting', '12');
231+
const identifier = recordIdentifierFor(userSetting);
232+
233+
const result = deleteRecord(userSetting);
198234

199235
assert.deepEqual(
200236
result,
201237
{
202-
url: 'https://api.example.com/api/v1/user-settings',
238+
url: 'https://api.example.com/api/v1/user-settings/12',
203239
method: 'DELETE',
204240
headers: new Headers(JSON_API_HEADERS),
205241
op: 'deleteRecord',
206242
data: {
207-
record,
243+
record: identifier,
208244
},
209245
},
210246
`deleteRecord works with patch option`
211247
);
212-
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
248+
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS, "headers are set to JSON API's");
213249
});
214250
});

0 commit comments

Comments
 (0)