Skip to content

Commit

Permalink
Added more integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Jul 6, 2015
1 parent bead196 commit 74c499c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ export default class Collection {
* @param {Object} options
* @return {Promise}
*/
create(record, options={synced: false}) {
create(record, options={forceUUID: false, synced: false}) {
return this.open().then(() => {
if (typeof(record) !== "object")
return Promise.reject(new Error('Record is not an object.'));
return new Promise((resolve, reject) => {
const {transaction, store} = this.prepare("readwrite");
const newRecord = Object.assign({}, record, {
id: options.synced ? record.id : uuid4(),
id: options.synced || options.forceUUID ? record.id : uuid4(),
_status: options.synced ? "synced" : "created"
});
store.add(newRecord);
Expand Down
14 changes: 11 additions & 3 deletions test/collection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,17 @@ describe("Collection", () => {
});

it("should actually persist the record into the collection", () => {
return articles.create(article).then(result => {
return articles.get(result.data.id).then(res => res.data.title);
}).should.become(article.title);
return articles.create(article)
.then(result => articles.get(result.data.id))
.then(res => res.data.title)
.should.become(article.title);
});

it("should support the forceUUID option", function() {
return articles.create({id: 42, title: "foo"}, {forceUUID: true})
.then(result => articles.get(result.data.id))
.then(res => res.data.id)
.should.become(42);
});

it("should prefix error encountered", () => {
Expand Down
126 changes: 62 additions & 64 deletions test/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import btoa from "btoa";
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import Cliquetis from "../src";
import { cleanRecord } from "../src/api";

chai.use(chaiAsPromised);
chai.should();
chai.config.includeStack = true;

const TEST_KINTO_SERVER = "http://0.0.0.0:8888/v1";

describe("Integration tests", () => {
describe.only("Integration tests", () => {
var tasks;

beforeEach(() => {
Expand All @@ -29,77 +30,74 @@ describe("Integration tests", () => {
});
});

describe("Synchronization", () => {
const fixtures = [
{title: "task1", done: true},
{title: "task2", done: false},
{title: "task3", done: false},
];

beforeEach(() => {
return Promise.all(fixtures.map(fixture => {
return tasks.create(fixture);
}));
function testSync(data) {
return Promise.all([].concat(
// Create local unsynced records
data.localUnsynced.map(record => tasks.create(record, {forceUUID: true})),
// Create local synced records
data.localSynced.map(record => tasks.create(record, {synced: true})),
// Create remote records
tasks.api.batch("default", "tasks", data.server)
)).then(_ => {
return tasks.sync();
});
}

describe("Synchronization", () => {
describe("No conflict", () => {
const testData = {
localSynced: [
{id: uuid4(), title: "task2", done: false},
{id: uuid4(), title: "task3", done: true},
],
localUnsynced: [
{id: uuid4(), title: "task4", done: false},
],
server: [
{id: uuid4(), title: "task1", done: true},
]
};
var syncResult;

describe("local updates", function() {
it("should update local records from server response", () => {
return tasks.sync()
.then(res => res.updated.map(r => r.title))
.should.eventually
.include("task1")
.include("task2")
.include("task3");
beforeEach(() => {
return testSync(testData).then(res => syncResult = res);
});

it("should publish local records to the server", () => {
return tasks.sync()
.then(res => res.published.map(r => r.title))
.should.eventually
.include("task1")
.include("task2")
.include("task3");
it("should have an ok status", () => {
expect(syncResult.ok).eql(true);
});

describe("Importing new remote records", function() {
var syncResult, createdId;

beforeEach(() => {
createdId = uuid4();
return tasks.api.batch("default", "tasks", [
{id: createdId, title: "task4", done: true}
])
.then(_ => tasks.sync())
.then(res => syncResult = res);
});

it("should list created records", function() {
expect(syncResult.created).to.have.length.of(1);
expect(syncResult.created[0].id).eql(createdId);
expect(syncResult.created[0].title).eql("task4");
});

it("should import records", function() {
return tasks.list()
.then(res => res.data.map(r => r.title))
.should.eventually
.include("task1")
.include("task2")
.include("task3")
.include("task4");
});
it("should contain no errors", () => {
expect(syncResult.errors).to.have.length.of(0);
});

it("should have a valid lastModified value", () => {
expect(syncResult.lastModified).to.be.a("number");
});

it("should not contain conflicts", () => {
expect(syncResult.conflicts).to.have.length.of(0);
});

it("should not have skipped records", () => {
expect(syncResult.skipped).to.have.length.of(0);
});

it("should have imported server data", () => {
expect(syncResult.created).to.have.length.of(1);
expect(cleanRecord(syncResult.created[0])).eql(testData.server[0]);
});

it("should have published local unsynced records", () => {
expect(syncResult.published).to.have.length.of(1);
expect(cleanRecord(syncResult.published[0])).eql(testData.localUnsynced[0]);
});
});

describe("remote updates", function() {
it("should have updated the server", function() {
return tasks.sync()
.then(_ => tasks.api.fetchChangesSince("default", "tasks"))
.then(res => res.changes.map(r => r.title))
.should.eventually
.include("task1")
.include("task2")
.include("task3");
it("should mark local records as synced", () => {
expect(syncResult.updated).to.have.length.of(2);
expect(syncResult.updated.map(r => cleanRecord(r))).to
.include(testData.server[0])
.include(testData.localUnsynced[0]);
});
});
});
Expand Down

0 comments on commit 74c499c

Please sign in to comment.