Skip to content

Commit 479938e

Browse files
add types
1 parent f51d78b commit 479938e

File tree

7 files changed

+151
-6
lines changed

7 files changed

+151
-6
lines changed

common/types-api.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var factory = function() {
2+
return {};
3+
};
4+
5+
var schema = {
6+
type: 'object',
7+
patternProperties: {
8+
'.*_.*': {
9+
type: 'object',
10+
properties: {
11+
reactions: {
12+
type: 'object'
13+
},
14+
tops: {
15+
type: 'array',
16+
minItems: 1,
17+
items: require('./types-post')
18+
},
19+
summary:{
20+
type: 'object'
21+
}
22+
},
23+
required: ['reactions', 'tops', 'summary']
24+
}
25+
}
26+
};
27+
28+
module.exports = {
29+
schema: schema,
30+
factory: factory
31+
};

common/types-post.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var factory = function() {
2+
return {}
3+
};
4+
5+
var schema = {
6+
type: 'object',
7+
properties: {
8+
id: {
9+
type: 'string',
10+
pattern: '\\d+_\\d+'
11+
},
12+
type:{
13+
enum : require('./fb-api').REACTION_TYPES
14+
},
15+
count: {
16+
type: 'number'
17+
},
18+
message:{
19+
type: 'string'
20+
},
21+
caption: {
22+
type: 'string'
23+
}
24+
}
25+
};
26+
27+
module.exports = {
28+
schema: schema,
29+
factory: factory
30+
};

common/types-reactions.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var factory = function() {
2+
return {}
3+
};
4+
5+
var schema = {
6+
type: 'object',
7+
patternProperties: {
8+
'.*': { type: 'integer', default: 0 }
9+
}
10+
};
11+
12+
module.exports = {
13+
schema: schema,
14+
factory: factory
15+
};

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "mocha test/**.spec.js",
88
"deploy": "apex -s FB_APP_ID='$FB_APP_ID' -s FB_APP_SECRET=='$FB_APP_SECRET' deploy"
99
},
1010
"author": "",
1111
"license": "MIT",
12+
"eslint": {
13+
"extends": "google",
14+
"rules": {}
15+
},
1216
"dependencies": {
1317
"bluebird": "^3.3.5",
1418
"chai": "^3.5.0",
1519
"dotenv": "^2.0.0",
1620
"limiter": "^1.1.0",
1721
"lodash": "*",
22+
"mocha": "^3.2.0",
1823
"moment": "^2.13.0",
1924
"redis": "^2.6.0-1",
2025
"request-promise": "*",
2126
"winston": "^2.2.0"
27+
},
28+
"devDependencies": {
29+
"eslint": "^3.11.1",
30+
"eslint-config-google": "^0.7.1"
2231
}
2332
}

test/fixture-api.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/types.spec.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var Ajv = require('ajv');
2+
var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
3+
var ReactionsType = require('../common/types-reactions');
4+
var ApiType = require('../common/types-api');
5+
var PostType = require('../common/types-post')
6+
var fixture = require('./fixture-api.js');
7+
describe('types', function() {
8+
9+
describe('reactions', function() {
10+
it('validate reaction types ', function () {
11+
var reactions = _.cloneDeep(fixture["1480348800000_1480435199999"].reactions);
12+
var isValid = ajv.validate(ReactionsType.schema, reactions);
13+
console.error(ajv.errors);
14+
expect(isValid).to.be.true;
15+
});
16+
it('invalidate wrong reaction types ', function () {
17+
var reactions = _.cloneDeep(fixture["1480348800000_1480435199999"].reactions);
18+
reactions.HAHA = '0';
19+
var isValid = ajv.validate(ReactionsType.schema, reactions);
20+
console.error(ajv.errors);
21+
expect(isValid).to.be.false;
22+
});
23+
});
24+
25+
describe('post', function() {
26+
it('validate post', function () {
27+
var post = _.cloneDeep(fixture["1480348800000_1480435199999"].tops)[0];
28+
var isValid = ajv.validate(PostType.schema, post);
29+
console.error(ajv.errors);
30+
expect(isValid).to.be.true;
31+
});
32+
it('invalidate incorrect post', function () {
33+
var post = _.cloneDeep(fixture["1480348800000_1480435199999"].tops)[0];
34+
post.type = 'HIHI'
35+
var isValid = ajv.validate(PostType.schema, post);
36+
console.error(ajv.errors);
37+
expect(isValid).to.be.false;
38+
});
39+
});
40+
41+
describe('api', function() {
42+
it('validate api responses', function () {
43+
var apiResponse = _.cloneDeep(fixture);
44+
var isValid = ajv.validate(ApiType.schema, apiResponse);
45+
console.error(ajv.errors);
46+
expect(isValid).to.be.true;
47+
});
48+
49+
it('invalidate incorrect api responses', function () {
50+
var apiResponse = _.cloneDeep(fixture);
51+
apiResponse["1480348800000_1480435199999"].tops = null
52+
var isValid = ajv.validate(ApiType.schema, apiResponse);
53+
console.error(ajv.errors);
54+
expect(isValid).to.be.false;
55+
});
56+
57+
});
58+
});

ui/src/app/fetchReactions.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { FETCH_AGG, fetchAndUpdateSelectedDate } from './store/actions'
22

3-
function getEndpoint (location, offset) {
4-
var url = 'https://8zbfsx31e0.execute-api.ap-northeast-1.amazonaws.com/prod/reactions'
5-
url += '?location=' + location
6-
url += '&offset=' + offset || 0
7-
return url
3+
const API_ENDPOINT = 'https://8zbfsx31e0.execute-api.ap-northeast-1.amazonaws.com/prod/reactions'
4+
5+
function getEndpoint (location, offset) {
6+
offset = offset || 0
7+
location = location || ''
8+
return `${API_ENDPOINT}?location=${location}&offset=${offset}`
89
}
910

1011
export default (dispatch) => function (offset) {

0 commit comments

Comments
 (0)