Browse Source

utils for mocking api responses in tests

Rafał Pitoń 10 years ago
parent
commit
5ab9e44b5b

+ 101 - 0
misago/emberapp/tests/helpers/api-mocks.js

@@ -0,0 +1,101 @@
+import Ember from 'ember';
+
+export function updateObjProps(obj, props) {
+  if (props) {
+    return Ember.$.extend({}, obj, props);
+  } else {
+    return Ember.$.extend({}, obj);
+  }
+}
+
+export function paginatedJSON(results, count, page, per_page, orphans) {
+  count = count || 0;
+  var pages = 0;
+
+  if (count && per_page) {
+    pages = Math.ceil(count / per_page);
+  }
+
+  if (orphans && pages > 1 && count - (pages - 1) * per_page <= orphans) {
+    pages -= 1;
+  }
+
+  var next = null;
+  var last = null;
+  if (page < pages) {
+    last = pages;
+    if (page + 1 < pages) {
+      next = page + 1;
+    }
+  }
+
+  var previous = null;
+  var first = null;
+  if (page > 1) {
+    first = 1;
+    if (page > 2) {
+      previous = page - 1;
+    }
+  }
+
+  var before = 0;
+  if (page > 1) {
+    before = (page - 1) * per_page;
+  }
+
+  var more = 0;
+  if (last) {
+    more = count - before - per_page;
+  }
+
+  return {
+    'results': results || [],
+
+    'count': count,
+    'pages': pages,
+    'next': next,
+    'last': last,
+    'previous': previous,
+    'first': first,
+    'before': before,
+    'more': more
+  };
+}
+
+export function rankJSON(id, props) {
+    var mock = {
+      'id': id,
+      'name': 'Members',
+      'slug': 'members',
+      'description': '',
+      'title': '',
+      'css_class': '',
+      'is_tab': false
+    };
+
+    return updateObjProps(mock, props);
+}
+
+export function userJSON(id, props) {
+  var mock = {
+    'id': id,
+    'username': 'MockedUser' + id,
+    'slug': 'mockeduser' + id,
+    'avatar_hash': 'h4sh',
+    'title': null,
+    'rank': rankJSON(1),
+    'state': {
+      'is_offline_hidden': false,
+      'is_online_hidden': false,
+      'is_offline': true,
+      'is_online': false,
+      'is_banned': false,
+      'last_click': '2015-07-05T16:06:44.010Z',
+      'is_hidden': false,
+      'banned_until': null
+    },
+    'signature': null
+  };
+
+  return updateObjProps(mock, props);
+}

+ 82 - 0
misago/emberapp/tests/unit/utils/api-mocks-test.js

@@ -0,0 +1,82 @@
+import { updateObjProps, paginatedJSON } from '../../helpers/api-mocks';
+import { module, test } from 'qunit';
+
+module('api-mocks');
+
+test('updateObjProps updates obj props', function(assert) {
+  assert.expect(4);
+
+  var testObj = { a: 123, b: { c: 123} };
+
+  assert.equal(updateObjProps(testObj).a, 123);
+  assert.ok(updateObjProps(testObj) !== testObj);
+  assert.equal(updateObjProps(testObj, { b: 'test' }).a, 123);
+  assert.equal(updateObjProps(testObj, { b: 'test' }).b, 'test');
+});
+
+test('paginatedJSON turns array into pagination response', function(assert) {
+  assert.expect(49);
+
+  var results = ['a', 'b', 'c', 'd', 'e', 'f'];
+  assert.equal(paginatedJSON(results).results, results);
+
+  var pagination = paginatedJSON(results, 12, 1, 8, 4);
+  assert.equal(pagination.count, 12);
+  assert.equal(pagination.pages, 1);
+  assert.equal(pagination.next, null);
+  assert.equal(pagination.last, null);
+  assert.equal(pagination.previous, null);
+  assert.equal(pagination.first, null);
+  assert.equal(pagination.before, 0);
+  assert.equal(pagination.more, 0);
+
+  pagination = paginatedJSON(results, 20, 1, 8, 4);
+  assert.equal(pagination.count, 20);
+  assert.equal(pagination.pages, 2);
+  assert.equal(pagination.next, null);
+  assert.equal(pagination.last, 2);
+  assert.equal(pagination.previous, null);
+  assert.equal(pagination.first, null);
+  assert.equal(pagination.before, 0);
+  assert.equal(pagination.more, 12);
+
+  pagination = paginatedJSON(results, 20, 2, 8, 4);
+  assert.equal(pagination.count, 20);
+  assert.equal(pagination.pages, 2);
+  assert.equal(pagination.next, null);
+  assert.equal(pagination.last, null);
+  assert.equal(pagination.previous, null);
+  assert.equal(pagination.first, 1);
+  assert.equal(pagination.before, 8);
+  assert.equal(pagination.more, 0);
+
+  pagination = paginatedJSON(results, 30, 2, 8, 4);
+  assert.equal(pagination.count, 30);
+  assert.equal(pagination.pages, 4);
+  assert.equal(pagination.next, 3);
+  assert.equal(pagination.last, 4);
+  assert.equal(pagination.previous, null);
+  assert.equal(pagination.first, 1);
+  assert.equal(pagination.before, 8);
+  assert.equal(pagination.more, 14);
+
+  pagination = paginatedJSON(results, 30, 3, 8, 4);
+  assert.equal(pagination.count, 30);
+  assert.equal(pagination.pages, 4);
+  assert.equal(pagination.next, null);
+  assert.equal(pagination.last, 4);
+  assert.equal(pagination.previous, 2);
+  assert.equal(pagination.first, 1);
+  assert.equal(pagination.before, 16);
+  assert.equal(pagination.more, 6);
+
+  pagination = paginatedJSON(results, 20, 3, 6, 2);
+  assert.equal(pagination.count, 20);
+  assert.equal(pagination.pages, 3);
+  assert.equal(pagination.next, null);
+  assert.equal(pagination.last, null);
+  assert.equal(pagination.previous, 2);
+  assert.equal(pagination.first, 1);
+  assert.equal(pagination.before, 12);
+  assert.equal(pagination.more, 0);
+});