Files
ewelink-api/test/env-node.spec.js
Martin M c11b3a8ab7 Release v3.0.0 (#85)
* updated dependencies

* code linting

* added new app id & app secret

* cleanup requests payloads

* remove unused function

* update test cases

* enabled firmware tests

* refactor getDevice to use right api endpoint

* error messages improvements

* error messages improvements

* error messages improvements

* error messages improvements

* error messages improvements

* payload cleanup

* refactor setDevicePowerState to use right api endpoint

* update test exepectation

* removed deprecated class

* updated tests to reflect new error codes

* error messages improvements

* refactoring project structure: devices methods

refactoring project sturcture

* refactoring project structure: firmware methods

* refactoring project structure: temperature/humidity

* refactoring project structure: credentials methods

* refactoring project structure: power usage methods

* refactoring project structure: power state methods

* refactoring project structure: websocket methods

* removed deprecated login method from docs

* refactoring project structure: power usage methods

* refactoring project structure: zeroconf classes

* refactoring project structure: websocket classes

* refactoring project structure: zeroconf classes

* refactor and cleanup

* refactoring project structure: firmware methods

* moved parsers to own directory

* update tests with methods renames

* export missing temperature/humidity methods

* removed unused package

* refactor and cleanup

* fix test expectation

* refactoring project structure: moved data files

* refactoring project structure: moved data files

* refactoring project structure: moved helpers files

* refactoring project structure: moved helpers files

* refactoring project structure: moved payload files

* refactor and cleanup

* refactor getDevicePowerState

* setDevicePowerState returns channel

* convert error 400 to 404 for clarity

* updated test cases

* remove console.log

* cache path for zeroconf cache files

* installed nock

* using nock to simulate server requests during testing

* moved credentials file to config folder

* update request url when using nock

* refactor nock helper file

* move cooldown delay to setupTests file

* updating testing instructions

* restored delete code block

* fix wrong error code

* accept phone number to login to ewelink

* added test cases for initialize main class

* improvements on class initialization parameters

* allow login using phone number

* rename test file

* updated test case

* fixed regression bug

* Release v3.0.0 - use node-fetch (#87)

* replaced deprecated request library with node-fetch

* refactor: moved makeRequest to own mixin file

* refactor to use node-fetch

* fixes

* update config

* created helper method

* constant rename

* ignore files from final package

* version bump
2020-05-23 03:07:52 -03:00

146 lines
5.4 KiB
JavaScript

const ewelink = require('../main');
const {
email,
password,
singleChannelDeviceId,
fourChannelsDevice,
} = require('./_setup/config/credentials.js');
const {
credentialsExpectations,
allDevicesExpectations,
specificDeviceExpectations,
} = require('./_setup/expectations');
describe('env: node script', () => {
let conn;
beforeAll(() => {
conn = new ewelink({ email, password });
});
test('get ewelink credentials', async () => {
const credentials = await conn.getCredentials();
expect(typeof credentials).toBe('object');
expect(credentials).toMatchObject(credentialsExpectations);
});
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);
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);
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);
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);
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);
});
test('get channel count 1', async () => {
const result = await conn.getDeviceChannelCount(singleChannelDeviceId);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.switchesAmount).toBe(1);
});
test('get channel count 4', async () => {
const result = await conn.getDeviceChannelCount(fourChannelsDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.switchesAmount).toBe(4);
});
});