contact.accessory.spec.ts 2.1 KB
Newer Older
1
#!/usr/bin/env ts-node
2 3
import test  from 'blue-tape'
// import sinon from 'sinon'
4

5 6 7 8 9 10
import {
  cloneClass,
}                           from 'clone-class'
import {
  MemoryCard,
}                           from 'memory-card'
11

12 13 14 15 16
import {
  Contact as GlobalContact,
}                           from './contact'
import {
  PuppetMock,
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
17
}                           from 'wechaty-puppet-mock'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
18 19 20

// tslint:disable-next-line:variable-name
const Contact = cloneClass(GlobalContact)
21 22

test('Should not be able to instanciate directly', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23 24
  // tslint:disable-next-line:variable-name
  const MyContact = cloneClass(Contact)
25
  t.throws(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26
    const c = MyContact.load('xxx')
27
    t.fail(c.name())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
28
  }, 'should throw when `Contact.load()`')
29 30

  t.throws(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
31
    const c = MyContact.load('xxx')
32 33 34 35 36 37 38 39 40
    t.fail(c.name())
  }, 'should throw when `Contact.load()`')
})

test('Should not be able to instanciate through cloneClass without puppet', async t => {
  // tslint:disable-next-line:variable-name
  const MyContact = cloneClass(Contact)

  t.throws(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41
    const c = MyContact.load('xxx')
42
    t.fail(c.name())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
43
  }, 'should throw when `MyContact.load()` without puppet')
44 45 46 47 48 49 50 51

  t.throws(() => {
    const c = MyContact.load('xxx')
    t.fail(c.name())
  }, 'should throw when `MyContact.load()` without puppet')

})

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52
test('should be able to instanciate through cloneClass with puppet', async t => {
53 54
  // tslint:disable-next-line:variable-name
  const MyContact = cloneClass(Contact)
55 56

  MyContact.puppet = new PuppetMock({ memory: new MemoryCard })
57 58

  t.doesNotThrow(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
59 60 61
    const c = MyContact.load('xxx')
    t.ok(c, 'should get contact instance from `MyContact.load()')
  }, 'should not throw when `MyContact().load`')
62 63 64 65 66 67 68

  t.doesNotThrow(() => {
    const c = MyContact.load('xxx')
    t.ok(c, 'should get contact instance from `MyContact.load()`')
  }, 'should not throw when `MyContact.load()`')

})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69 70 71 72 73 74 75 76

test('should throw when instanciate the global class', async t => {
  t.throws(() => {
    const c = GlobalContact.load('xxx')
    t.fail('should not run to here')
    t.fail(c.toString())
  }, 'should throw when we instanciate a global class')
})