Skip to content

Commit

Permalink
ui: Add basic timestamp method we can access from tests, fixup tests
Browse files Browse the repository at this point in the history
Adds a timestamp method that we can access from within tests so we can
test that the SyncTime is being set.

There is probably a better way to do this, but this is also probably the
simplest approach - we are also likely to revisit this at a later date
  • Loading branch information
John Cowen committed Apr 30, 2019
1 parent c6c6d92 commit d43947f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
7 changes: 5 additions & 2 deletions ui-v2/app/serializers/application.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Serializer from 'ember-data/serializers/rest';

import { get, set } from '@ember/object';
import { set } from '@ember/object';
import {
HEADERS_SYMBOL as HTTP_HEADERS_SYMBOL,
HEADERS_INDEX as HTTP_HEADERS_INDEX,
Expand Down Expand Up @@ -44,12 +44,15 @@ export default Serializer.extend({
requestType
);
},
timestamp: function() {
return new Date().getTime();
},
normalizeMeta: function(store, primaryModelClass, headers, payload, id, requestType) {
const meta = {
cursor: headers[HTTP_HEADERS_INDEX],
};
if (requestType === 'query') {
meta.date = new Date().getTime();
meta.date = this.timestamp();
payload.forEach(function(item) {
set(item, 'SyncTime', meta.date);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { moduleFor, test } from 'ember-qunit';
import repo from 'consul-ui/tests/helpers/repo';
import { get } from '@ember/object';
const NAME = 'coordinate';
moduleFor(`service:repository/${NAME}`, `Integration | Service | ${NAME}`, {
// Specify the other units that are required for this test.
integration: true
integration: true,
});

const dc = 'dc-1';
const now = new Date().getTime();
test('findAllByDatacenter returns the correct data for list endpoint', function(assert) {
get(this.subject(), 'store').serializerFor(NAME).timestamp = function() {
return now;
};
return repo(
'Coordinate',
'findAllByDatacenter',
Expand All @@ -26,6 +31,7 @@ test('findAllByDatacenter returns the correct data for list endpoint', function(
expected(function(payload) {
return payload.map(item =>
Object.assign({}, item, {
SyncTime: now,
Datacenter: dc,
uid: `["${dc}","${item.Node}"]`,
})
Expand Down
7 changes: 6 additions & 1 deletion ui-v2/tests/integration/services/repository/node-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { moduleFor, test } from 'ember-qunit';
import repo from 'consul-ui/tests/helpers/repo';
import { get } from '@ember/object';
const NAME = 'node';
moduleFor(`service:repository/${NAME}`, `Integration | Service | ${NAME}`, {
// Specify the other units that are required for this test.
Expand All @@ -8,7 +9,11 @@ moduleFor(`service:repository/${NAME}`, `Integration | Service | ${NAME}`, {

const dc = 'dc-1';
const id = 'token-name';
const now = new Date().getTime();
test('findByDatacenter returns the correct data for list endpoint', function(assert) {
get(this.subject(), 'store').serializerFor(NAME).timestamp = function() {
return now;
};
return repo(
'Node',
'findAllByDatacenter',
Expand All @@ -27,6 +32,7 @@ test('findByDatacenter returns the correct data for list endpoint', function(ass
expected(function(payload) {
return payload.map(item =>
Object.assign({}, item, {
SyncTime: now,
Datacenter: dc,
uid: `["${dc}","${item.ID}"]`,
})
Expand Down Expand Up @@ -56,7 +62,6 @@ test('findBySlug returns the correct data for item endpoint', function(assert) {
Datacenter: dc,
uid: `["${dc}","${item.ID}"]`,
meta: {
date: undefined,
cursor: undefined,
},
});
Expand Down
7 changes: 6 additions & 1 deletion ui-v2/tests/integration/services/repository/service-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { moduleFor, test } from 'ember-qunit';
import { skip } from 'qunit';
import repo from 'consul-ui/tests/helpers/repo';
import { get } from '@ember/object';
const NAME = 'service';
moduleFor(`service:repository/${NAME}`, `Integration | Service | ${NAME}`, {
// Specify the other units that are required for this test.
integration: true,
});
const dc = 'dc-1';
const id = 'token-name';
const now = new Date().getTime();
test('findByDatacenter returns the correct data for list endpoint', function(assert) {
get(this.subject(), 'store').serializerFor(NAME).timestamp = function() {
return now;
};
return repo(
'Service',
'findAllByDatacenter',
Expand All @@ -27,6 +32,7 @@ test('findByDatacenter returns the correct data for list endpoint', function(ass
expected(function(payload) {
return payload.map(item =>
Object.assign({}, item, {
SyncTime: now,
Datacenter: dc,
uid: `["${dc}","${item.Name}"]`,
})
Expand Down Expand Up @@ -69,7 +75,6 @@ test('findBySlug returns the correct data for item endpoint', function(assert) {
service.Nodes = nodes;
service.Tags = payload.Nodes[0].Service.Tags;
service.meta = {
date: undefined,
cursor: undefined,
};

Expand Down
6 changes: 6 additions & 0 deletions ui-v2/tests/integration/services/repository/session-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { moduleFor, test } from 'ember-qunit';
import repo from 'consul-ui/tests/helpers/repo';
import { get } from '@ember/object';
const NAME = 'session';
moduleFor(`service:repository/${NAME}`, `Integration | Service | ${NAME}`, {
// Specify the other units that are required for this test.
Expand All @@ -8,7 +9,11 @@ moduleFor(`service:repository/${NAME}`, `Integration | Service | ${NAME}`, {

const dc = 'dc-1';
const id = 'node-name';
const now = new Date().getTime();
test('findByNode returns the correct data for list endpoint', function(assert) {
get(this.subject(), 'store').serializerFor(NAME).timestamp = function() {
return now;
};
return repo(
'Session',
'findByNode',
Expand All @@ -27,6 +32,7 @@ test('findByNode returns the correct data for list endpoint', function(assert) {
expected(function(payload) {
return payload.map(item =>
Object.assign({}, item, {
SyncTime: now,
Datacenter: dc,
uid: `["${dc}","${item.ID}"]`,
})
Expand Down

0 comments on commit d43947f

Please sign in to comment.