config.spec.ts 2.7 KB
Newer Older
1
#!/usr/bin/env ts-node
2
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
 *   Wechaty - https://github.com/chatie/wechaty
4
 *
5
 *   @copyright 2016-2017 Huan LI <zixia@zixia.net>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
18 19
 *
 */
20 21 22 23
// tslint:disable:no-shadowed-variable
import * as test  from 'blue-tape'
// import * as sinon from 'sinon'
// const sinonTest   = require('sinon-test')(sinon)
24

25
import { config } from './config'
26
import { Puppet } from './puppet'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27

28
test('important variables', async t => {
29 30 31 32
  t.true('puppet'   in config, 'should exist `puppet` in Config')
  t.true('apihost'  in config, 'should exist `apihost` in Config')
  t.true('profile'  in config, 'should exist `profile` in Config')
  t.true('token'    in config, 'should exist `token` in Config')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
33

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
34 35 36 37
  t.ok(config.default.DEFAULT_PUPPET      , 'should export DEFAULT_PUPPET')
  t.ok(config.default.DEFAULT_PROFILE     , 'should export DEFAULT_PROFILE')
  t.ok(config.default.DEFAULT_PROTOCOL    , 'should export DEFAULT_PROTOCOL')
  t.ok(config.default.DEFAULT_APIHOST     , 'should export DEFAULT_APIHOST')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38 39
})

40
test('validApiHost()', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41
  const OK_APIHOSTS = [
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
42 43
    'api.chatie.io',
    'chatie.io:8080',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44 45
  ]
  const ERR_APIHOSTS = [
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46 47
    'https://api.chatie.io',
    'chatie.io/',
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
48 49
  ]
  OK_APIHOSTS.forEach(apihost => {
50
    t.doesNotThrow(() => {
51
      config.validApiHost(apihost)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52 53
    })
  }, 'should not row for right apihost')
54
  ERR_APIHOSTS.forEach(apihost => {
55
    t.throws(() => {
56
      config.validApiHost(apihost)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57 58 59
    })
  }, 'should throw for error apihost')

60
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
61

62 63 64 65 66 67
test('puppetInstance()', async t => {
  // BUG Compitable with Win32 CI
  // global instance infected across unit tests... :(
  const bak = config.puppetInstance()

  config.puppetInstance(null)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
68
  t.throws(() => {
69
    config.puppetInstance()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70
  }, Error, 'should throw when not initialized')
71
  config.puppetInstance(bak)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
72

73 74
  const EXPECTED = <Puppet>{userId: 'test'}
  const mockPuppet = EXPECTED
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
75

76 77
  config.puppetInstance(mockPuppet)
  const instance = config.puppetInstance()
78
  t.deepEqual(instance, EXPECTED, 'should equal with initialized data')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
79

80
  config.puppetInstance(null)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
81
  t.throws(() => {
82
    config.puppetInstance()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
83 84
  }, Error, 'should throw after set to null')

85
  config.puppetInstance(bak)
Huan (李卓桓)'s avatar
lint  
Huan (李卓桓) 已提交
86
})