contact.spec.ts 2.2 KB
Newer Older
1 2
#!/usr/bin/env ts-node

3
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
4
 *   Wechaty - https://github.com/chatie/wechaty
5
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6
 *   @copyright 2016-2018 Huan LI <zixia@zixia.net>
7 8 9 10 11 12 13 14 15 16 17 18
 *
 *   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.
19 20
 *
 */
21 22 23 24
// tslint:disable:no-shadowed-variable
import * as test  from 'blue-tape'
// import * as sinon from 'sinon'

25
import config     from '../src/config'
Huan (李卓桓)'s avatar
merge  
Huan (李卓桓) 已提交
26
import Contact    from '../src/contact'
27
import Profile    from '../src/profile'
Huan (李卓桓)'s avatar
merge  
Huan (李卓桓) 已提交
28
import PuppetWeb  from '../src/puppet-web'
29

30 31 32
config.puppetInstance(new PuppetWeb({
  profile: new Profile(),
}))
33

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
34
test('Contact smoke testing', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35
  /* tslint:disable:variable-name */
36
  const UserName = '@0bb3e4dd746fdbd4a80546aef66f4085'
ruiruibupt's avatar
1  
ruiruibupt 已提交
37 38
  const NickName = 'NickNameTest'
  const RemarkName = 'AliasTest'
39 40

  // Mock
41
  const mockContactGetter = function (id) {
42
    return new Promise<any>((resolve, reject) => {
Huan (李卓桓)'s avatar
lint  
Huan (李卓桓) 已提交
43
      if (id !== UserName) return resolve({})
44
      setTimeout(() => {
45
        return resolve({
L
lijiarui 已提交
46 47 48
          UserName: UserName,
          NickName: NickName,
          RemarkName: RemarkName,
49 50 51 52 53 54 55
        })
      }, 200)
    })
  }

  const c = new Contact(UserName)

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
56 57 58 59
  t.is(c.id, UserName, 'id/UserName right')
  const r = await c.ready(mockContactGetter)
  t.is(r.get('id')   , UserName, 'UserName set')
  t.is(r.get('name') , NickName, 'NickName set')
ruiruibupt's avatar
ruiruibupt 已提交
60 61
  t.is(r.name(), NickName, 'should get the right name from Contact')
  t.is(r.alias(), RemarkName, 'should get the right alias from Contact')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
62

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63 64
  const s = r.toString()
  t.is(typeof s, 'string', 'toString()')
ruiruibupt's avatar
1  
ruiruibupt 已提交
65 66 67 68 69 70

  // const contact1 = await Contact.find({name: 'NickNameTest'})
  // t.is(contact1.id, UserName, 'should find contact by name')

  // const contact2 = await Contact.find({alias: 'AliasTest'})
  // t.is(contact2.id, UserName, 'should find contact by alias')
71
})