Skip to content

Commit

Permalink
Move *Queries test to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
manuquentin committed May 15, 2015
1 parent a01234c commit 000f5e2
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 322 deletions.
4 changes: 2 additions & 2 deletions src/javascripts/ng-admin/es6/lib/Queries/ReadQueries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Queries from './Queries'

class RetrieveQueries extends Queries {
class ReadQueries extends Queries {

/**
* Get one entity
Expand Down Expand Up @@ -233,4 +233,4 @@ class RetrieveQueries extends Queries {
};
}

export default RetrieveQueries
export default ReadQueries
182 changes: 182 additions & 0 deletions src/javascripts/ng-admin/es6/tests/lib/Queries/ReadQueriesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
var assert = require('chai').assert;
var sinon = require('sinon');

import ReadQueries from "../../../lib/Queries/ReadQueries";
import DataStore from "../../../lib/DataStore/DataStore";
import PromisesResolver from "../../mock/PromisesResolver";
import Entity from "../../../lib/Entity/Entity";
import ReferenceField from "../../../lib/Field/ReferenceField";
import TextField from "../../../lib/Field/TextField";
import Field from "../../../lib/Field/Field";
import buildPromise from "../../mock/mixins";

describe('ReadQueries', () => {
var readQueries,
restWrapper = {},
application = {},
rawCats,
rawHumans,
catEntity,
humanEntity,
catView;

beforeEach(() => {
application = {
getRouteFor: (view, id) => {
let url = 'http://localhost/' + view.getEntity().name();
if (id) {
url += '/' + id;
}

return url;
}
};

readQueries = new ReadQueries(restWrapper, PromisesResolver, application);
catEntity = new Entity('cat');
humanEntity = new Entity('human');
catView = catEntity.views["ListView"]
.addField(new TextField('name'))
.addField(new ReferenceField('human_id').targetEntity(humanEntity).targetField(new Field('firstName')));

humanEntity.identifier(new Field('id'));

rawCats = [
{"id": 1, "human_id": 1, "name": "Mizoute", "summary": "A Cat"},
{"id": 2, "human_id": 1, "name": "Suna", "summary": "A little Cat"}
];

rawHumans = [
{"id": 1, "firstName": "Daph"},
{"id": 2, "firstName": "Manu"},
{"id": 3, "firstName": "Daniel"}
];
});

describe("getOne", () => {

it('should return the entity with all fields.', () => {
var entity = new Entity('cat');
var view = entity.views['ListView']
.addField(new Field('id').identifier(true))
.addField(new TextField('name'));

restWrapper.getOne = sinon.stub().returns(buildPromise({
data: {
"id": 1,
"name": "Mizoute",
"summary": "A Cat"
}
}));

readQueries.getOne(view, 1)
.then((rawEntry) => {
assert(restWrapper.getOne.calledWith('cat', 'http://localhost/cat/1'));

assert.equal(rawEntry.data.id, 1);
assert.equal(rawEntry.data.name, 'Mizoute');

// Non mapped field should also be retrieved
assert.equal(rawEntry.data.summary, "A Cat");
});
});

});

describe('getAll', () => {
it('should return all data to display a ListView', () => {
restWrapper.getList = sinon.stub().returns(buildPromise({data: rawCats, headers: () => {}}));
PromisesResolver.allEvenFailed = sinon.stub().returns(buildPromise([
{status: 'success', result: rawHumans[0] },
{status: 'success', result: rawHumans[1] },
{status: 'success', result: rawHumans[2] }
]));

readQueries.getAll(catView)
.then((result) => {
assert.equal(result.totalItems, 2);
assert.equal(result.data.length, 2);

assert.equal(result.data[0].id, 1);
assert.equal(result.data[0].name, 'Mizoute');

assert.equal(result.data[0].human_id, 1);
});
});
});

describe('getReferencedData', () => {
it('should return all references data for a View with multiple calls', () => {
var post = new Entity('posts'),
author = new Entity('authors'),
authorRef = new ReferenceField('author');

var rawPosts = [
{id: 1, author: 'abc'},
{id: 2, author: '19DFE'}
];

var rawAuthors = [
{id: 'abc', name: 'Rollo'},
{id: '19DFE', name: 'Ragna'}
];

authorRef.targetEntity(author);
authorRef.targetField(new Field('name'));
post.views["ListView"]
.addField(authorRef);

restWrapper.getOne = sinon.stub().returns(buildPromise({}));
PromisesResolver.allEvenFailed = sinon.stub().returns(buildPromise([
{status: 'success', result: rawAuthors[0] },
{ status: 'success', result: rawAuthors[1] }
]));

readQueries.getReferencedData(post.views["ListView"].getReferences(), rawPosts)
.then((referencedData) => {
assert.equal(referencedData.author.length, 2);
assert.equal(referencedData.author[0].id, 'abc');
assert.equal(referencedData.author[1].name, 'Ragna');
});
});

it('should return all references data for a View with one call', () => {
var post = new Entity('posts'),
author = new Entity('authors'),
authorRef = new ReferenceField('author');

authorRef.singleApiCall((ids) => {
return {
id: ids
};
});

var rawPosts = [
{id: 1, author: 'abc'},
{id: 2, author: '19DFE'}
];

var rawAuthors = [
{id: 'abc', name: 'Rollo'},
{id: '19DFE', name: 'Ragna'}
];

authorRef.targetEntity(author);
authorRef.targetField(new Field('name'));
post.views["ListView"]
.addField(authorRef);

restWrapper.getList = sinon.stub().returns(buildPromise({data: rawCats, headers: () => {}}));
PromisesResolver.allEvenFailed = sinon.stub().returns(buildPromise([
{status: 'success', result: { data: rawAuthors }}
]));

readQueries.getReferencedData(post.views["ListView"].getReferences(), rawPosts)
.then((referencedData) => {
assert.equal(referencedData['author'].length, 2);
assert.equal(referencedData['author'][0].id, 'abc');
assert.equal(referencedData['author'][1].name, 'Ragna');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import WriteQueries from "../../../lib/Queries/WriteQueries";
import DataStore from "../../../lib/DataStore/DataStore";
import PromisesResolver from "../../mock/PromisesResolver";
import Entity from "../../../lib/Entity/Entity";
import NumberField from "../../../lib/Field/NumberField";
import TextField from "../../../lib/Field/TextField";
import buildPromise from "../../mock/mixins";

describe.only('WriteQueries', function() {
describe('WriteQueries', () => {
var writeQueries,
restWrapper = {},
application = {},
entity,
view;

beforeEach(function() {
beforeEach(() => {
application = {
getRouteFor: (view, id) => {
let url = 'http://localhost/' + view.getEntity().name();
Expand All @@ -31,11 +30,10 @@ describe.only('WriteQueries', function() {
writeQueries = new WriteQueries(restWrapper, PromisesResolver, application);
entity = new Entity('cat');
view = entity.views["CreateView"]
.addField(new NumberField('id').identifier(true))
.addField(new TextField('name'));
});

describe('createOne', function() {
describe('createOne', () => {
it('should POST an entity when calling createOne', () => {
var rawEntity = {name: 'Mizu'};

Expand All @@ -52,7 +50,7 @@ describe.only('WriteQueries', function() {
});
});

describe('updateOne', function() {
describe('updateOne', () => {
var rawEntity = {id: 3, name: 'Mizu'},
updatedEntity = {id: 3, name: 'Mizute'};

Expand Down Expand Up @@ -81,7 +79,7 @@ describe.only('WriteQueries', function() {
});
});

describe("deleteOne", function () {
describe("deleteOne", () => {
restWrapper.deleteOne = sinon.stub().returns(buildPromise({}));

it('should DELETE an entity when calling deleteOne', () => {
Expand All @@ -92,8 +90,8 @@ describe.only('WriteQueries', function() {
});
});

describe("batchDelete", function () {
it('should DELETE entities when calling batchEntities', function () {
describe("batchDelete", () => {
it('should DELETE entities when calling batchEntities', () => {
restWrapper.deleteOne = sinon.stub().returns(buildPromise({}));

writeQueries.batchDelete(view, [1, 2])
Expand Down
69 changes: 0 additions & 69 deletions src/javascripts/test/unit/Crud/repository/DeleteQueriesSpec.js

This file was deleted.

Loading

0 comments on commit 000f5e2

Please sign in to comment.