summaryrefslogtreecommitdiff
path: root/node_modules/node-wifi/src/utils/__test__/promiser.spec.js
blob: b2754e4bf58274981ff450803b9defedda633b3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const promiser = require('../promiser');

const config = { foo: 'foo' };

describe('promiser', () => {
  it('should execute function without error and return promise if no callback provided', async () => {
    const func = jest.fn(() => Promise.resolve('bar'));

    const result = await promiser(func)(config)();

    expect(func).toHaveBeenCalledWith(config);
    expect(result).toEqual('bar');
  });

  it('should execute function with error and return promise if no callback provided', async () => {
    expect.assertions(2);

    const func = jest.fn(() => Promise.reject('error'));
    try {
      await promiser(func)(config)();
    } catch (error) {
      expect(func).toHaveBeenCalledWith(config);
      expect(error).toEqual('error');
    }
  });

  it('should execute function without error and call callback', done => {
    const func = jest.fn(() => Promise.resolve('bar'));

    promiser(func)(config)((error, result) => {
      expect(func).toHaveBeenCalledWith(config);
      expect(result).toEqual('bar');
      done();
    });
  });

  it('should execute function with error and return promise if no callback provided', done => {
    const func = jest.fn(() => Promise.reject('error'));
    promiser(func)(config)(error => {
      expect(func).toHaveBeenCalledWith(config);
      expect(error).toEqual('error');
      done();
    });
  });

  it('should execute function without error with all args and return promise if no callback provided', async () => {
    const func = jest.fn(() => Promise.resolve('bar'));
    const arg = { ssid: 'foo', password: 'bar' };

    const result = await promiser(func)(config)(arg);

    expect(func).toHaveBeenCalledWith(config, arg);
    expect(result).toEqual('bar');
  });

  it('should execute function without error and call callback', done => {
    const func = jest.fn(() => Promise.resolve('bar'));
    const arg = { ssid: 'foo', password: 'bar' };

    promiser(func)(config)(arg, (error, result) => {
      expect(func).toHaveBeenCalledWith(config, arg);
      expect(result).toEqual('bar');
      done();
    });
  });
});