|
1 | 1 | import { module, test } from 'qunit';
|
2 | 2 |
|
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'; |
4 | 6 | import { setBuildURLConfig } from '@ember-data/request-utils';
|
| 7 | +import Store, { recordIdentifierFor } from '@ember-data/store'; |
5 | 8 |
|
| 9 | +import UserSetting from '../../app/models/user-setting'; |
6 | 10 | import { headersToObject } from '../helpers/utils';
|
7 | 11 |
|
8 | 12 | const ACTIVE_RECORD_HEADERS = { 'content-type': 'application/json; charset=utf-8' };
|
9 | 13 |
|
10 | 14 | module('ActiveRecord | Request Builders', function (hooks) {
|
| 15 | + setupTest(hooks); |
| 16 | + |
11 | 17 | hooks.beforeEach(function () {
|
12 | 18 | setBuildURLConfig({ host: 'https://api.example.com', namespace: 'api/v1' });
|
13 | 19 | });
|
@@ -109,4 +115,161 @@ module('ActiveRecord | Request Builders', function (hooks) {
|
109 | 115 | );
|
110 | 116 | assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS);
|
111 | 117 | });
|
| 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 | + }); |
112 | 275 | });
|
0 commit comments