Skip to content

Instantly share code, notes, and snippets.

@al3xnag
Created March 22, 2022 07:50
Show Gist options
  • Save al3xnag/4da2dc6812e1597904436681abc62f47 to your computer and use it in GitHub Desktop.
Save al3xnag/4da2dc6812e1597904436681abc62f47 to your computer and use it in GitHub Desktop.
ed race cond
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
export default class extends Model {
@attr('string') title;
}
<h1>Welcome to {{this.appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { assign } from '@ember/polyfills';
import { start } from 'ember-qunit';
let attributes = {
rootElement: '#test-root',
autoboot: false
};
attributes = assign(attributes, config.APP);
let application = Application.create(attributes);
setApplication(application);
start();
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import Pretender from 'pretender';
module('TODO: put something here', function(hooks) {
setupTest(hooks);
test('services/store/with-socks :: race condition: no ember-data issue', async function(assert) {
const store = this.owner.lookup('service:store');
const socksPublishContext = {
channel: 'system',
data: '{"courses": [{"id": "1", "title": "test course 1"}]}',
};
const serverResponse = [
201,
{ 'Content-Type': 'application/json' },
JSON.stringify({
courses: [{ id: 1, title: 'test course 1' }]
})
];
let courseViaRest;
let courseViaWS;
const server = new Pretender(function() {
this.post('/api/courses', (req) => {
return new Promise(resolve => {
//socks.onPublish(socksPublishContext);
store.pushPayload('course', {
courses: [{ id: 1, title: 'test course 1' }]
});
courseViaWS = store.peekRecord('course', 1);
assert.equal(courseViaWS.id, '1');
assert.equal(courseViaWS.title, 'test course 1');
assert.equal(courseViaWS.isLoaded, true);
assert.equal(courseViaWS.isSaving, false);
assert.ok(courseViaRest);
assert.notEqual(courseViaRest, courseViaWS);
assert.equal(courseViaRest.isSaving, true);
assert.equal(courseViaRest.id, null);
setTimeout(() => resolve(serverResponse), 100);
});
});
});
try {
courseViaRest = store.createRecord('course', { title: 'test course 1' });
await courseViaRest.save();
assert.equal(courseViaRest.id, '1');
assert.equal(courseViaRest.title, 'test course 1');
assert.equal(courseViaRest.isLoaded, true);
assert.notEqual(courseViaRest, courseViaWS);
assert.equal(courseViaWS.id, '1');
assert.equal(courseViaWS.title, 'test course 1');
} finally {
server.shutdown();
}
});
});
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-data": "3.4.2",
"ember-cli-pretender": "^3.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment