Skip to content

Commit

Permalink
[BUGFIX inspector] Fix columns names in debug-adapter (#5733)
Browse files Browse the repository at this point in the history
* [BUGFIX inspector] Include primaryKey column and default to 'id'

* [BUGFIX inspector] Always use `id` for primary key

* [CHORE tests] modernize integration/debug-adapter-test

* [BUGFIX inspector] Revert changes to id

* [BUGFIX inspector] Add test for column names
  • Loading branch information
thorsteinsson authored and runspired committed Nov 5, 2018
1 parent 0986ed1 commit b142626
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 111 deletions.
10 changes: 9 additions & 1 deletion addon/-private/system/debug/debug-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export default DataAdapter.extend({
return typeClass !== Model && Model.detect(typeClass);
},

columnNameToDesc(name) {
return capitalize(
underscore(name)
.replace(/_/g, ' ')
.trim()
);
},

columnsForType(typeClass) {
let columns = [
{
Expand All @@ -44,7 +52,7 @@ export default DataAdapter.extend({
if (count++ > self.attributeLimit) {
return false;
}
let desc = capitalize(underscore(name).replace('_', ' '));
let desc = this.columnNameToDesc(name);
columns.push({ name: name, desc: desc });
});
return columns;
Expand Down
210 changes: 100 additions & 110 deletions tests/integration/debug-adapter-test.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,52 @@
import { setupTest } from 'ember-qunit';
import { A } from '@ember/array';
import Application from '@ember/application';
import { get } from '@ember/object';
import { run } from '@ember/runloop';

import Model from 'ember-data/model';
import { attr } from '@ember-decorators/data';
import Adapter from 'ember-data/adapter';
import { module, test } from 'qunit';
import { settled } from '@ember/test-helpers';

import DS from 'ember-data';

let App, store, debugAdapter;

module('DS.DebugAdapter', {
beforeEach() {
run(function() {
App = Application.extend({
toString() {
return 'debug-app';
},
}).create();

App.StoreService = DS.Store.extend({});

App.ApplicationAdapter = DS.Adapter.extend({
shouldBackgroundReloadRecord: () => false,
});
class Post extends Model {
@attr
title;
}

App.Post = DS.Model.extend({
title: DS.attr('string'),
});

// TODO: Remove this when Ember is upgraded to >= 1.13
App.Post.reopenClass({
_debugContainerKey: 'model:post',
});
});
module('integration/debug-adapter - DS.DebugAdapter', function(hooks) {
setupTest(hooks);

store = App.__container__.lookup('service:store');
debugAdapter = App.__container__.lookup('data-adapter:main');
let store, debugAdapter;

let klass;
hooks.beforeEach(function() {
let { owner } = this;

if (App.__container__.factoryFor) {
klass = App.__container__.factoryFor('model:post').class;
} else {
klass = App.__container__.lookupFactory('model:post');
}
owner.register('model:post', Post);
store = owner.lookup('service:store');
debugAdapter = owner.lookup('data-adapter:main');

debugAdapter.reopen({
getModelTypes() {
return A([{ klass, name: 'post' }]);
return A([{ klass: Post, name: 'post' }]);
},
});
},
afterEach() {
run(App, App.destroy);
App = store = null;
},
});
});

test('Watching Model Types', function(assert) {
assert.expect(5);
test('Watching Model Types', async function(assert) {
assert.expect(5);

function added(types) {
assert.equal(types.length, 1);
assert.equal(types[0].name, 'post');
assert.equal(types[0].count, 0);
assert.strictEqual(types[0].object, store.modelFor('post'));
}
function added(types) {
assert.equal(types.length, 1);
assert.equal(types[0].name, 'post');
assert.equal(types[0].count, 0);
assert.strictEqual(types[0].object, store.modelFor('post'));
}

function updated(types) {
assert.equal(types[0].count, 1);
}
function updated(types) {
assert.equal(types[0].count, 1);
}

debugAdapter.watchModelTypes(added, updated);
debugAdapter.watchModelTypes(added, updated);

run(() => {
store.push({
data: {
type: 'post',
Expand All @@ -84,12 +57,19 @@ test('Watching Model Types', function(assert) {
},
});
});
});

test('Watching Records', function(assert) {
var post, record, addedRecords, updatedRecords, removedIndex, removedCount;
test('Watching Records', async function(assert) {
let addedRecords, updatedRecords, removedIndex, removedCount;

this.owner.register(
'adapter:application',
Adapter.extend({
shouldBackgroundReloadRecord() {
return false;
},
})
);

run(function() {
store.push({
data: {
type: 'post',
Expand All @@ -99,60 +79,70 @@ test('Watching Records', function(assert) {
},
},
});
});

var recordsAdded = function(wrappedRecords) {
addedRecords = wrappedRecords;
};
var recordsUpdated = function(wrappedRecords) {
updatedRecords = wrappedRecords;
};
var recordsRemoved = function(index, count) {
removedIndex = index;
removedCount = count;
};

let modelClassOrName;
if (debugAdapter.get('acceptsModelName')) {
modelClassOrName = 'post';
} else {
modelClassOrName = App.__container__.lookupFactory('model:post');
}
debugAdapter.watchRecords(modelClassOrName, recordsAdded, recordsUpdated, recordsRemoved);

assert.equal(get(addedRecords, 'length'), 1);
record = addedRecords[0];
assert.deepEqual(record.columnValues, { id: '1', title: 'Clean Post' });
assert.deepEqual(record.filterValues, { isNew: false, isModified: false, isClean: true });
assert.deepEqual(record.searchKeywords, ['1', 'Clean Post']);
assert.deepEqual(record.color, 'black');

run(function() {
post = store.findRecord('post', 1);
});
var recordsAdded = function(wrappedRecords) {
addedRecords = wrappedRecords;
};
var recordsUpdated = function(wrappedRecords) {
updatedRecords = wrappedRecords;
};
var recordsRemoved = function(index, count) {
removedIndex = index;
removedCount = count;
};

debugAdapter.watchRecords('post', recordsAdded, recordsUpdated, recordsRemoved);

assert.equal(get(addedRecords, 'length'), 1);
let record = addedRecords[0];
assert.deepEqual(record.columnValues, { id: '1', title: 'Clean Post' });
assert.deepEqual(record.filterValues, { isNew: false, isModified: false, isClean: true });
assert.deepEqual(record.searchKeywords, ['1', 'Clean Post']);
assert.deepEqual(record.color, 'black');

let post = await store.findRecord('post', 1);

run(function() {
post.set('title', 'Modified Post');
});

assert.equal(get(updatedRecords, 'length'), 1);
record = updatedRecords[0];
assert.deepEqual(record.columnValues, { id: '1', title: 'Modified Post' });
assert.deepEqual(record.filterValues, { isNew: false, isModified: true, isClean: false });
assert.deepEqual(record.searchKeywords, ['1', 'Modified Post']);
assert.deepEqual(record.color, 'blue');
assert.equal(get(updatedRecords, 'length'), 1);
record = updatedRecords[0];
assert.deepEqual(record.columnValues, { id: '1', title: 'Modified Post' });
assert.deepEqual(record.filterValues, { isNew: false, isModified: true, isClean: false });
assert.deepEqual(record.searchKeywords, ['1', 'Modified Post']);
assert.deepEqual(record.color, 'blue');

post = store.createRecord('post', { id: '2', title: 'New Post' });

await settled();

post = store.createRecord('post', { id: '2', title: 'New Post' });
assert.equal(get(addedRecords, 'length'), 1);
record = addedRecords[0];
assert.deepEqual(record.columnValues, { id: '2', title: 'New Post' });
assert.deepEqual(record.filterValues, { isNew: true, isModified: false, isClean: false });
assert.deepEqual(record.searchKeywords, ['2', 'New Post']);
assert.deepEqual(record.color, 'green');

assert.equal(get(addedRecords, 'length'), 1);
record = addedRecords[0];
assert.deepEqual(record.columnValues, { id: '2', title: 'New Post' });
assert.deepEqual(record.filterValues, { isNew: true, isModified: false, isClean: false });
assert.deepEqual(record.searchKeywords, ['2', 'New Post']);
assert.deepEqual(record.color, 'green');
post.unloadRecord();

run(post, 'unloadRecord');
await settled();

assert.equal(removedIndex, 1);
assert.equal(removedCount, 1);
assert.equal(removedIndex, 1);
assert.equal(removedCount, 1);
});

test('Column names', function(assert) {
class Person extends Model {
@attr
title;

@attr
firstOrLastName;
}

const columns = debugAdapter.columnsForType(Person);

assert.equal(columns[0].desc, 'Id');
assert.equal(columns[1].desc, 'Title');
assert.equal(columns[2].desc, 'First or last name');
});
});

0 comments on commit b142626

Please sign in to comment.