How to use the testdouble.when function in testdouble

To help you get started, we’ve selected a few testdouble examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mysql / mysql-connector-nodejs / test / unit / Protocol / Protobuf / Expect.js View on Github external
it('returns a Buffer-encoded version of a Mysqlx.Expect.Open object containing the entire list of expectations', () => {
                const expectations = ['foo', 'bar'];

                td.when(createCondition('foo')).thenReturn('baz');
                td.when(createCondition('bar')).thenReturn('qux');
                td.when(FakeOpenStub.prototype.serializeBinary()).thenReturn('quux');

                const open = Expect.encodeOpen(expectations);

                // eslint-disable-next-line node/no-deprecated-api
                expect(open).to.deep.equal(new Buffer('quux'));
                expect(td.explain(FakeOpenStub.prototype.setCondList).callCount).to.equal(1);
                return expect(td.explain(FakeOpenStub.prototype.setCondList).calls[0].args[0]).to.deep.equal(['baz', 'qux']);
            });
        });
github ethereumjs / ethereumjs-client / test / net / server / libp2pserver.js View on Github external
tape('[Libp2pServer]', t => {
  const PeerInfo = td.replace('peer-info')
  const PeerId = td.replace('peer-id')
  const Libp2pPeer = td.replace('../../../lib/net/peer/libp2ppeer')
  Libp2pPeer.prototype.id = 'id0'
  class Libp2pNode extends EventEmitter {}
  Libp2pNode.prototype.handle = td.func()
  Libp2pNode.prototype.start = td.func()
  Libp2pNode.prototype.stop = td.func()
  Libp2pNode.prototype.peerInfo = { multiaddrs: { toArray: () => ['ma0'] } }
  td.when(Libp2pNode.prototype.handle('/proto/1')).thenCallback(null, 'conn0')
  td.when(Libp2pNode.prototype.handle('/proto/2')).thenCallback(null, 'conn1')
  td.when(Libp2pNode.prototype.start()).thenCallback()
  td.when(Libp2pNode.prototype.stop()).thenCallback()
  td.replace('../../../lib/net/peer/libp2pnode', Libp2pNode)
  const Libp2pServer = require('../../../lib/net/server/libp2pserver')
  const peerInfo = { multiaddrs: { add: td.func(), toArray: td.func() }, id: { toB58String: td.func() } }
  const peerInfo0 = { multiaddrs: { add: td.func() } }
  td.when(PeerId.createFromPrivKey(1)).thenCallback(null, 'id0')
  td.when(PeerId.createFromPrivKey(2)).thenCallback(null, 'id1')
  td.when(PeerId.createFromPrivKey(3)).thenCallback('err1')
  td.when(PeerInfo.create('id0')).thenCallback(null, peerInfo0)
  td.when(PeerInfo.create('id1')).thenCallback('err0')
  td.when(PeerInfo.create()).thenCallback(null, peerInfo)
  td.when(peerInfo.id.toB58String()).thenReturn('id')

  t.test('should initialize correctly', async (t) => {
    const server = new Libp2pServer({
      multiaddrs: 'ma0,ma1',
github ethereumjs / ethereumjs-client / test / net / protocol / boundprotocol.js View on Github external
t.test('should perform request', async (t) => {
    const sender = new EventEmitter()
    const bound = new BoundProtocol({ protocol, peer, sender })
    sender.sendMessage = td.func()
    td.when(protocol.encode(testMessage, 1)).thenReturn('1')
    td.when(protocol.decode(testResponse, '2')).thenReturn(2)
    td.when(sender.sendMessage(0x01, '1')).thenDo(() => {
      setTimeout(() => {
        sender.emit('message', { code: 0x02, payload: '2' })
      }, 100)
    })
    const response = await bound.testMessage(1)
    t.equals(response, 2, 'got response')
    td.when(protocol.decode(testResponse, '2')).thenThrow(new Error('error1'))
    try {
      await bound.testMessage(1)
    } catch (err) {
      t.ok(/error1/.test(err.message), 'got error')
    }
    t.end()
  })
github joyent / cloudapi-gql / test / random.js View on Github external
it('can recursively get a random name if a name matches an existing machine', async () => {
    const fetch = TestDouble.func();
    const machineCaptor = TestDouble.matchers.captor();
    const randomNameCaptor = TestDouble.matchers.captor();
    TestDouble.when(fetch(TestDouble.matchers.anything(), machineCaptor.capture())).thenResolve([{id: 'bacon'}], []);
    const Sentiment = TestDouble.replace('sentiment');
    TestDouble.when(Sentiment(randomNameCaptor.capture())).thenReturn({ score: 1 });
    const Random = require('../lib/handlers/random');
    await Random.rndName(fetch);
    expect(machineCaptor.values.length).to.equal(2);
    expect(randomNameCaptor.values.length).to.equal(2);
  });
github mysql / mysql-connector-nodejs / test / unit / Protocol / Client.js View on Github external
it('sends a Mysqlx.Session.Close message to the server', () => {
                const client = new FakeClient(network);

                td.when(encodeClose()).thenReturn('foo');
                td.when(encodeMessage(ClientMessages.Type.SESS_CLOSE, 'foo')).thenReturn('bar');
                td.when(FakeOkHandler.prototype.sendMessage(td.matchers.anything(), td.matchers.anything(), 'bar')).thenResolve('baz');

                return client.sessionClose()
                    .then(actual => expect(actual).to.equal('baz'));
            });
github ethereumjs / ethereumjs-client / test / net / server / libp2pserver.js View on Github external
Libp2pNode.prototype.peerInfo = { multiaddrs: { toArray: () => ['ma0'] } }
  td.when(Libp2pNode.prototype.handle('/proto/1')).thenCallback(null, 'conn0')
  td.when(Libp2pNode.prototype.handle('/proto/2')).thenCallback(null, 'conn1')
  td.when(Libp2pNode.prototype.start()).thenCallback()
  td.when(Libp2pNode.prototype.stop()).thenCallback()
  td.replace('../../../lib/net/peer/libp2pnode', Libp2pNode)
  const Libp2pServer = require('../../../lib/net/server/libp2pserver')
  const peerInfo = { multiaddrs: { add: td.func(), toArray: td.func() }, id: { toB58String: td.func() } }
  const peerInfo0 = { multiaddrs: { add: td.func() } }
  td.when(PeerId.createFromPrivKey(1)).thenCallback(null, 'id0')
  td.when(PeerId.createFromPrivKey(2)).thenCallback(null, 'id1')
  td.when(PeerId.createFromPrivKey(3)).thenCallback('err1')
  td.when(PeerInfo.create('id0')).thenCallback(null, peerInfo0)
  td.when(PeerInfo.create('id1')).thenCallback('err0')
  td.when(PeerInfo.create()).thenCallback(null, peerInfo)
  td.when(peerInfo.id.toB58String()).thenReturn('id')

  t.test('should initialize correctly', async (t) => {
    const server = new Libp2pServer({
      multiaddrs: 'ma0,ma1',
      bootnodes: 'boot0,boot1',
      key: 'abcd'
    })
    t.deepEquals(server.multiaddrs, [ 'ma0', 'ma1' ], 'multiaddrs split')
    t.deepEquals(server.bootnodes, [ 'boot0', 'boot1' ], 'bootnodes split')
    t.equals(server.key.toString('base64'), 'abcd', 'key is correct')
    t.equals(server.name, 'libp2p', 'get name')
    t.end()
  })

  t.test('should create peer info', async (t) => {
    let server = new Libp2pServer({ multiaddrs: 'ma0' })
github artlogic / nabs / test / Task.js View on Github external
it('should concatenate the passed action with .action', () => {
      let action;
      const task = new nabs.Task([]);

      action = ['one', 'two', 'three'];
      td.when(nabs.makeArray(action)).thenReturn(action);
      task.addAction(action);
      task.actions.should.eql(action);

      td.when(nabs.makeArray([])).thenReturn([]);
      task.addAction([]);
      task.actions.should.eql(action);

      action = action.concat(['four']);
      td.when(nabs.makeArray(['four'])).thenReturn(['four']);
      task.addAction(['four']);
      task.actions.should.eql(action);
    });
  });
github ethereumjs / ethereumjs-client / test / sync / fetcher / headerfetcher.js View on Github external
t.test('should find a fetchable peer', async (t) => {
    const pool = new PeerPool()
    const fetcher = new HeaderFetcher({ pool })
    td.when(fetcher.pool.idle(td.matchers.anything())).thenReturn('peer0')
    t.equals(fetcher.peer(), 'peer0', 'found peer')
    t.end()
  })
github testdouble / test-smells / smells / unrealistic / surreal / surreal.js View on Github external
addsWeights: function () {
    var smallWetSock = new Clothing('S', 'sock', 'wet')
    var largeDryJacket = new Clothing('L', 'jacket', 'dry')
    td.when(td.replace(largeDryJacket, 'weight')()).thenReturn(8)
    var xlSoakedPants = td.object(['weight'])
    td.when(xlSoakedPants.weight()).thenReturn(15)

    var result = weighClothes([smallWetSock, largeDryJacket, xlSoakedPants])

    assert.equal(result, 26)
  },
  afterEach: function () {
github Automattic / google-docs-add-on / test / doc-service.js View on Github external
function containerOf( ...elements ) {
	const container = td.object( ['getNumChildren', 'getChild', 'getType', 'getPreviousSibling', 'getNextSibling', 'getAttributes', 'getPositionedImages'] )

	elements.forEach( ( el, i ) => {
		td.when( container.getChild( i ) ).thenReturn( el )
		td.when( el.getPreviousSibling() ).thenReturn( elements[i - 1] )
		td.when( el.getNextSibling() ).thenReturn( elements[i + 1] )
	} )
	td.when( container.getNumChildren() ).thenReturn( elements.length )
	td.when( container.getPositionedImages() ).thenReturn( [] )

	return container
}