Skip to content

Commit d5c57c3

Browse files
authored
[updates] More unit tests (jest), CI (circle), etc (dollarshaveclub#61)
* [bug] fixes spelling * [update] adds more tests; config updates * [init] adds initial circle setup * [update] adds tests: messageId/log fn * [update] init sanitize tests
1 parent 0a3abed commit d5c57c3

File tree

5 files changed

+66
-17
lines changed

5 files changed

+66
-17
lines changed

.circleci/config.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defaults: &defaults
2+
working_directory: ~/code
3+
docker:
4+
- image: circleci/node:8
5+
environment:
6+
NPM_CONFIG_LOGLEVEL: error # make npm commands less noisy
7+
JOBS: max # https://gist.github.com/ralphtheninja/f7c45bdee00784b41fed
8+
9+
restore_cache: &restore_cache
10+
keys:
11+
- code-{{ .Branch }}-{{ checksum ".nvmrc" }}-{{ checksum "package.json" }}
12+
- code-master-{{ checksum ".nvmrc" }}-{{ checksum "package.json" }}
13+
14+
save_cache: &save_cache
15+
key: code-{{ .Branch }}-{{ checksum ".nvmrc" }}-{{ checksum "package.json" }}
16+
paths:
17+
- node_modules
18+
19+
version: 2
20+
jobs:
21+
build:
22+
<<: *defaults
23+
steps:
24+
- checkout
25+
- restore_cache: *restore_cache
26+
- run: npm install
27+
- run: npm run eslint:ci
28+
- run: npm test
29+
- run: npx codecov
30+
- save_cache: *save_cache

jsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
"vinyl-source-stream": "^2.0.0"
4040
},
4141
"scripts": {
42-
"eslint": "eslint . --fix .",
42+
"eslint": "eslint . --fix",
43+
"eslint:ci": "eslint .",
4344
"test": "npm run test:unit && npm run test:acceptance && npm run test:es-check",
4445
"test:es-check": "es-check es5 build/postmate.min.js",
45-
"test:unit": "nyc jest --coverage",
46-
"test:acceptance": "gulp test --coverage",
46+
"test:unit": "jest --coverage",
47+
"test:acceptance": "gulp test",
4748
"build": "npm run build:dist && npm run build:readme",
4849
"build:readme": "node ./scripts/update-readme.js",
4950
"build:dist": "rollup --config configs/rollup.config.js",

src/postmate.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* The type of messages our frames our sending
44
* @type {String}
55
*/
6-
export const messsageType = 'application/x-postmate-v1+json'
6+
export const messageType = 'application/x-postmate-v1+json'
77

88
/**
99
* hasOwnProperty()
@@ -22,13 +22,13 @@ export const maxHandshakeRequests = 5
2222
* A unique message ID that is used to ensure responses are sent to the correct requests
2323
* @type {Number}
2424
*/
25-
let _messageId = 0
25+
const _messageId = 0
2626

2727
/**
2828
* Increments and returns a message ID
2929
* @return {Number} A unique ID for a message
3030
*/
31-
export const messageId = () => ++_messageId
31+
export const messageId = () => _messageId + 1
3232

3333
/**
3434
* Postmate logging function that enables/disables via config
@@ -231,14 +231,12 @@ export class ChildAPI {
231231
* @type {Class}
232232
*/
233233
class Postmate {
234-
static debug = false // eslint-disable-line no-undef
235-
236234
// Internet Explorer craps itself
237235
static Promise = (() => {
238236
try {
239237
return window ? window.Promise : Promise
240238
} catch (e) {
241-
return null
239+
return log('Promise: error', e)
242240
}
243241
})()
244242

test/unit/test.postmate.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@ import {
33
log,
44
maxHandshakeRequests,
55
messageId,
6-
message_type,
6+
messageType,
77
ParentAPI,
88
Postmate,
99
resolveOrigin,
1010
resolveValue,
1111
sanitize,
1212
} from '../../src/postmate'
1313

14-
// Jest works
15-
test('Jest is working', () => expect(1).toBe(1))
14+
test('Message Type', () => expect(messageType).toBe('application/x-postmate-v1+json'))
15+
16+
test('messageId', () => expect(!isNaN(messageId())).toBe(true))
17+
18+
test('messageId adds 1', () => {
19+
const result = messageId()
20+
expect(result).toBe(1)
21+
})
22+
23+
test('postmate logs args', () => {
24+
Postmate.debug = true
25+
console.log = jest.fn()
26+
log('a', 'b', 'c')
27+
expect(typeof log !== 'undefined')
28+
expect(typeof log).toBe('function')
29+
expect(console.log.mock.calls[0][0]).toBe('a')
30+
expect(console.log.mock.calls[0][1]).toBe('b')
31+
expect(console.log.mock.calls[0][2]).toBe('c')
32+
})
33+
34+
test('Sanitize', () => {
35+
expect(typeof sanitize !== 'undefined')
36+
})
1637

1738
// test API
1839
// the tests below test the API generally
@@ -31,11 +52,6 @@ test('ChildAPI class is ready to rock', () => {
3152
expect(typeof ChildAPI.emit !== 'undefined')
3253
})
3354

34-
test('log func is ready to rock', () => {
35-
expect(typeof log !== 'undefined')
36-
expect(typeof log).toBe('function')
37-
})
38-
3955
test('maxHandshakeRequests class is ready to rock', () => {
4056
expect(maxHandshakeRequests === 5)
4157
})
@@ -55,6 +71,9 @@ test('ParentAPI class is ready to rock', () => {
5571

5672
test('resolveOrigin class is ready to rock', () => {
5773
expect(typeof resolveOrigin !== 'undefined')
74+
const result = 'https://sometest.com'
75+
const a = resolveOrigin(result)
76+
expect(a).toEqual(result)
5877
})
5978

6079
test('resolveValue class is ready to rock', () => {

0 commit comments

Comments
 (0)