Support saving posts via Store
This commit is contained in:
@@ -100,6 +100,8 @@ test("Create a Reply", () => {
|
||||
test("Edit the first post", () => {
|
||||
visit("/t/internationalization-localization/280");
|
||||
|
||||
ok(!exists('.topic-post:eq(0) .post-info.edits'), 'it has no edits icon at first');
|
||||
|
||||
click('.topic-post:eq(0) button[data-action=showMoreActions]');
|
||||
click('.topic-post:eq(0) button[data-action=edit]');
|
||||
andThen(() => {
|
||||
@@ -111,6 +113,7 @@ test("Edit the first post", () => {
|
||||
click('#reply-control button.create');
|
||||
andThen(() => {
|
||||
ok(!exists('#wmd-input'), 'it closes the composer');
|
||||
ok(exists('.topic-post:eq(0) .post-info.edits'), 'it has the edits icon');
|
||||
ok(find('#topic-title h1').text().indexOf('This is the new text for the title') !== -1, 'it shows the new title');
|
||||
ok(find('.topic-post:eq(0) .cooked').text().indexOf('This is the new text for the post') !== -1, 'it updates the post');
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export default {
|
||||
"/posts/398": {"id":398,"name":"Uwe Keim","username":"uwe_keim","avatar_template":"/user_avatar/meta.discourse.org/uwe_keim/{size}/5697.png","uploaded_avatar_id":5697,"created_at":"2013-02-05T21:29:00.280Z","cooked":"<p>Any plans to support localization of UI elements, so that I (for example) could set up a completely German speaking forum?</p>","post_number":1,"post_type":1,"updated_at":"2013-02-05T21:29:00.280Z","like_count":0,"reply_count":1,"reply_to_post_number":null,"quote_count":0,"avg_time":25,"incoming_link_count":314,"reads":475,"score":1702.25,"yours":false,"topic_id":280,"topic_slug":"internationalization-localization","display_username":"Uwe Keim","primary_group_name":null,"version":2,"can_edit":true,"can_delete":false,"can_recover":true,"user_title":null,"raw":"Any plans to support localization of UI elements, so that I (for example) could set up a completely German speaking forum?","actions_summary":[{"id":2,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":3,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":4,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":5,"count":0,"hidden":true,"can_act":true,"can_defer_flags":false},{"id":6,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":7,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":8,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false}],"moderator":false,"admin":false,"staff":false,"user_id":255,"hidden":false,"hidden_reason_id":null,"trust_level":2,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false}
|
||||
"/posts/398": {"id":398,"name":"Uwe Keim","username":"uwe_keim","avatar_template":"/user_avatar/meta.discourse.org/uwe_keim/{size}/5697.png","uploaded_avatar_id":5697,"created_at":"2013-02-05T21:29:00.280Z","cooked":"<p>Any plans to support localization of UI elements, so that I (for example) could set up a completely German speaking forum?</p>","post_number":1,"post_type":1,"updated_at":"2013-02-05T21:29:00.280Z","like_count":0,"reply_count":1,"reply_to_post_number":null,"quote_count":0,"avg_time":25,"incoming_link_count":314,"reads":475,"score":1702.25,"yours":false,"topic_id":280,"topic_slug":"internationalization-localization","display_username":"Uwe Keim","primary_group_name":null,"version":1,"can_edit":true,"can_delete":false,"can_recover":true,"user_title":null,"raw":"Any plans to support localization of UI elements, so that I (for example) could set up a completely German speaking forum?","actions_summary":[{"id":2,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":3,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":4,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":5,"count":0,"hidden":true,"can_act":true,"can_defer_flags":false},{"id":6,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":7,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false},{"id":8,"count":0,"hidden":false,"can_act":true,"can_defer_flags":false}],"moderator":false,"admin":false,"staff":false,"user_id":255,"hidden":false,"hidden_reason_id":null,"trust_level":2,"deleted_at":null,"user_deleted":false,"edit_reason":null,"can_view_edit_history":true,"wiki":false}
|
||||
};
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -2,15 +2,21 @@ function parsePostData(query) {
|
||||
const result = {};
|
||||
query.split("&").forEach(function(part) {
|
||||
const item = part.split("=");
|
||||
result[item[0]] = decodeURIComponent(item[1]).replace(/\+/g, ' ');
|
||||
const firstSeg = decodeURIComponent(item[0]);
|
||||
const m = /^([^\[]+)\[([^\]]+)\]/.exec(firstSeg);
|
||||
|
||||
const val = decodeURIComponent(item[1]).replace(/\+/g, ' ');
|
||||
if (m) {
|
||||
result[m[1]] = result[m[1]] || {};
|
||||
result[m[1]][m[2]] = val;
|
||||
} else {
|
||||
result[firstSeg] = val;
|
||||
}
|
||||
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
function clone(obj) {
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
function response(code, obj) {
|
||||
if (typeof code === "object") {
|
||||
obj = code;
|
||||
@@ -122,7 +128,10 @@ export default function() {
|
||||
this.put('/posts/:post_id/recover', success);
|
||||
|
||||
this.put('/posts/:post_id', (request) => {
|
||||
return response({ post: {id: request.params.post_id, version: 2 } });
|
||||
const data = parsePostData(request.requestBody);
|
||||
data.post.id = request.params.post_id;
|
||||
data.post.version = 2;
|
||||
return response(200, data.post);
|
||||
});
|
||||
|
||||
this.put('/t/:slug/:id', (request) => {
|
||||
@@ -157,9 +166,15 @@ export default function() {
|
||||
}
|
||||
});
|
||||
|
||||
this.post('/widgets', function(request) {
|
||||
const widget = parsePostData(request.requestBody).widget;
|
||||
widget.id = 100;
|
||||
return response(200, {widget});
|
||||
});
|
||||
|
||||
this.put('/widgets/:widget_id', function(request) {
|
||||
const w = _widgets.findBy('id', parseInt(request.params.widget_id));
|
||||
return response({ widget: clone(w) });
|
||||
const widget = parsePostData(request.requestBody).widget;
|
||||
return response({ widget });
|
||||
});
|
||||
|
||||
this.get('/widgets', function(request) {
|
||||
|
||||
@@ -28,6 +28,21 @@ test('update', function() {
|
||||
});
|
||||
});
|
||||
|
||||
test('save new', function() {
|
||||
const store = createStore();
|
||||
const widget = store.createRecord('widget');
|
||||
|
||||
ok(widget.get('isNew'), 'it is a new record');
|
||||
ok(!widget.get('isCreated'), 'it is not created');
|
||||
|
||||
widget.save({ name: 'Evil Widget' }).then(function() {
|
||||
ok(widget.get('id'), 'it has an id');
|
||||
ok(widget.get('name'), 'Evil Widget');
|
||||
ok(widget.get('isCreated'), 'it is created');
|
||||
ok(!widget.get('isNew'), 'it is no longer new');
|
||||
});
|
||||
});
|
||||
|
||||
test('destroyRecord', function() {
|
||||
const store = createStore();
|
||||
store.find('widget', 123).then(function(widget) {
|
||||
|
||||
@@ -5,6 +5,8 @@ import createStore from 'helpers/create-store';
|
||||
test('createRecord', function() {
|
||||
const store = createStore();
|
||||
const widget = store.createRecord('widget', {id: 111, name: 'hello'});
|
||||
|
||||
ok(!widget.get('isNew'), 'it is not a new record');
|
||||
equal(widget.get('name'), 'hello');
|
||||
equal(widget.get('id'), 111);
|
||||
});
|
||||
@@ -13,6 +15,7 @@ test('createRecord without an `id`', function() {
|
||||
const store = createStore();
|
||||
const widget = store.createRecord('widget', {name: 'hello'});
|
||||
|
||||
ok(widget.get('isNew'), 'it is a new record');
|
||||
ok(!widget.get('id'), 'there is no id');
|
||||
});
|
||||
|
||||
@@ -21,6 +24,7 @@ test('createRecord without attributes', function() {
|
||||
const widget = store.createRecord('widget');
|
||||
|
||||
ok(!widget.get('id'), 'there is no id');
|
||||
ok(widget.get('isNew'), 'it is a new record');
|
||||
});
|
||||
|
||||
test('createRecord with a record as attributes returns that record from the map', function() {
|
||||
@@ -36,6 +40,7 @@ test('find', function() {
|
||||
store.find('widget', 123).then(function(w) {
|
||||
equal(w.get('name'), 'Trout Lure');
|
||||
equal(w.get('id'), 123);
|
||||
ok(!w.get('isNew'), 'found records are not new');
|
||||
|
||||
// A second find by id returns the same object
|
||||
store.find('widget', 123).then(function(w2) {
|
||||
@@ -70,6 +75,7 @@ test('findAll', function() {
|
||||
store.findAll('widget').then(function(result) {
|
||||
equal(result.get('length'), 2);
|
||||
const w = result.findBy('id', 124);
|
||||
ok(!w.get('isNew'), 'found records are not new');
|
||||
equal(w.get('name'), 'Evil Repellant');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user