Files
ewelink-api/test/main-env-node.spec.js
Martin M 024aaa8a2c Release v1.4.1 (#13)
* specific error when requested channel is bigger than total

* fix for multi-channel devices

* updated test expectations

* fixes with uiid api changes

* fix wrong error message

* added new test cases

* return expected error on wrong channel number

* added more devices

* version bump
2019-09-11 23:46:17 -03:00

140 lines
5.0 KiB
JavaScript

const delay = require('delay');
const ewelink = require('../main');
const {
email,
password,
singleChannelDeviceId,
fourChannelsDevice,
} = require('./_setup/credentials.json');
const {
loginExpectations,
allDevicesExpectations,
specificDeviceExpectations,
} = require('./_setup/expectations');
describe('env: node script', () => {
let conn;
beforeAll(() => {
conn = new ewelink({ email, password });
});
beforeEach(async () => {
await delay(1000);
});
test('login into ewelink', async () => {
const login = await conn.login();
expect(typeof login).toBe('object');
expect(login).toMatchObject(loginExpectations);
});
test('get all devices', async () => {
const devices = await conn.getDevices();
expect(Array.isArray(devices)).toBe(true);
expect(devices[0]).toMatchObject(allDevicesExpectations);
});
test('get specific device', async () => {
const device = await conn.getDevice(singleChannelDeviceId);
expect(typeof device).toBe('object');
expect(device.deviceid).toBe(singleChannelDeviceId);
expect(device).toMatchObject(specificDeviceExpectations);
});
test('get single channel device power state', async () => {
const device = await conn.getDevice(singleChannelDeviceId);
const currentState = device.params.switch;
const powerState = await conn.getDevicePowerState(singleChannelDeviceId);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('get multi channel device power state', async () => {
const channel = 3;
const device = await conn.getDevice(fourChannelsDevice);
const currentState = device.params.switches[channel - 1].switch;
const powerState = await conn.getDevicePowerState(
fourChannelsDevice,
channel
);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(currentState);
});
test('set single channel device power state', async () => {
jest.setTimeout(30000);
await delay(3000);
const device = await conn.getDevice(singleChannelDeviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(
singleChannelDeviceId,
newState
);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(singleChannelDeviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
});
test('set multi channel device power state', async () => {
jest.setTimeout(30000);
await delay(3000);
const channel = 3;
const device = await conn.getDevice(fourChannelsDevice);
const currentState = device.params.switches[channel - 1].switch;
const newState = currentState === 'on' ? 'off' : 'on';
const powerState = await conn.setDevicePowerState(
fourChannelsDevice,
newState,
channel
);
expect(typeof powerState).toBe('object');
expect(powerState.status).toBe('ok');
expect(powerState.state).toBe(newState);
const deviceVerify = await conn.getDevice(fourChannelsDevice);
const currentStateVerify = deviceVerify.params.switches[channel - 1].switch;
expect(newState).toBe(currentStateVerify);
});
test('toggle single channel device power state', async () => {
jest.setTimeout(30000);
await delay(3000);
const device = await conn.getDevice(singleChannelDeviceId);
const currentState = device.params.switch;
const newState = currentState === 'on' ? 'off' : 'on';
await conn.toggleDevice(singleChannelDeviceId);
const deviceVerify = await conn.getDevice(singleChannelDeviceId);
const currentStateVerify = deviceVerify.params.switch;
expect(newState).toBe(currentStateVerify);
await conn.toggleDevice(singleChannelDeviceId);
const deviceVerifyAgain = await conn.getDevice(singleChannelDeviceId);
const currentStateVerifyAgain = deviceVerifyAgain.params.switch;
expect(currentState).toBe(currentStateVerifyAgain);
});
test('toggle multi channel device power state', async () => {
jest.setTimeout(30000);
await delay(3000);
const channel = 3;
const device = await conn.getDevice(fourChannelsDevice);
const currentState = device.params.switches[channel - 1].switch;
const newState = currentState === 'on' ? 'off' : 'on';
await conn.toggleDevice(fourChannelsDevice, channel);
const deviceVerify = await conn.getDevice(fourChannelsDevice);
const currentStateVerify = deviceVerify.params.switches[channel - 1].switch;
expect(newState).toBe(currentStateVerify);
await conn.toggleDevice(fourChannelsDevice, channel);
const deviceVerifyAgain = await conn.getDevice(fourChannelsDevice);
const currentStateVerifyAgain =
deviceVerifyAgain.params.switches[channel - 1].switch;
expect(currentState).toBe(currentStateVerifyAgain);
});
});