This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create, update and destroy methods on the AJAX Adapter.
- Loading branch information
Showing
15 changed files
with
289 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
// import test from "tape-catch"; | ||
// import Store from "../../../src/store"; | ||
import test from "tape-catch"; | ||
import sinon from "sinon"; | ||
import Store from "../../../src/store"; | ||
|
||
test("destroy must delete a resource from the server and remove it from the store on success", function (t) { | ||
var server = sinon.fakeServer.create({ autoRespond: true }); | ||
var adapter = new Store.AjaxAdapter(); | ||
var store = new Store(adapter); | ||
t.plan(2); | ||
t.timeoutAfter(1000); | ||
store.define("products", {}); | ||
server.respondWith("DELETE", "/products/6", [ | ||
204, | ||
{ | ||
"Content-Type": "application/vnd.api+json" | ||
}, | ||
"" | ||
]); | ||
store.add({ | ||
type: "products", | ||
id: "6" | ||
}); | ||
t.equal(store.find("products").length, 1); | ||
store.destroy(store.find("products", "6"), function () { | ||
t.equal(store.find("products").length, 0); | ||
}); | ||
server.restore(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
// import test from "tape-catch"; | ||
// import Store from "../../../src/store"; | ||
import test from "tape-catch"; | ||
import sinon from "sinon"; | ||
import Store from "../../../src/store"; | ||
|
||
test("update must update a resource on the server and add reflect the changes in the store on success", function (t) { | ||
var server = sinon.fakeServer.create({ autoRespond: true }); | ||
var adapter = new Store.AjaxAdapter(); | ||
var store = new Store(adapter); | ||
t.plan(2); | ||
t.timeoutAfter(1000); | ||
store.define("products", { | ||
title: Store.attr() | ||
}); | ||
server.respondWith("PATCH", "/products/9", [ | ||
204, | ||
{ | ||
"Content-Type": "application/vnd.api+json" | ||
}, | ||
"" | ||
]); | ||
store.add({ | ||
type: "products", | ||
id: "9", | ||
attributes: { | ||
title: "My Book" | ||
} | ||
}); | ||
store.update({ type: "products", id: "9", title: "My Book!" }, function (product) { | ||
t.equal(product.title, "My Book!"); | ||
t.equal(store.find("products", "9").title, "My Book!"); | ||
}); | ||
server.restore(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1 @@ | ||
import "./store/clud/create-spec"; | ||
import "./store/clud/destroy-spec"; | ||
import "./store/clud/load-spec"; | ||
import "./store/clud/update-spec"; | ||
import "./store/core/add-spec"; | ||
import "./store/core/define-spec"; | ||
import "./store/core/find-spec"; | ||
import "./store/core/push-spec"; | ||
import "./store/core/remove-spec"; | ||
import "./store/events/off-spec"; | ||
import "./store/events/on-spec"; | ||
import "./store/fields/attr-spec"; | ||
import "./store/fields/has-many-spec"; | ||
import "./store/fields/has-one-spec"; | ||
import "./shared"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import "./store/clud/create-spec"; | ||
import "./store/clud/destroy-spec"; | ||
import "./store/clud/load-spec"; | ||
import "./store/clud/update-spec"; | ||
import "./store/core/add-spec"; | ||
import "./store/core/convert-spec"; | ||
import "./store/core/define-spec"; | ||
import "./store/core/find-spec"; | ||
import "./store/core/push-spec"; | ||
import "./store/core/remove-spec"; | ||
import "./store/events/off-spec"; | ||
import "./store/events/on-spec"; | ||
import "./store/fields/attr-spec"; | ||
import "./store/fields/has-many-spec"; | ||
import "./store/fields/has-one-spec"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import test from "tape-catch"; | ||
import sinon from "sinon"; | ||
import Store from "../../../src/store"; | ||
|
||
test("convert must use serialize functions provided by type definitions", function (t) { | ||
var store = new Store(); | ||
var serialize = sinon.spy(function (resource, data) { | ||
data.attributes.title = resource.title + "!"; | ||
}); | ||
var resource = { | ||
type: "products", | ||
id: "44", | ||
title: "Example" | ||
}; | ||
t.plan(3); | ||
store.define("products", { | ||
title: { | ||
serialize: serialize | ||
} | ||
}); | ||
store.convert(resource).attributes.title | ||
t.equal(serialize.firstCall.args[0], resource); | ||
t.deepEqual(serialize.firstCall.args[1], { type: "products", id: "44", attributes: { title: "Example!" }, relationships: {} }); | ||
t.equal(serialize.firstCall.args[2], "title"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.