Files
ewelink-api/test/temperature-humidity.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

151 lines
5.2 KiB
JavaScript

const ewelink = require('../main');
const errors = require('../src/data/errors');
const {
email,
password,
deviceIdWithoutTempAndHum,
deviceIdWithTempAndHum: thDevice,
} = require('./_setup/config/credentials.js');
describe('current temperature and humidity: node script', () => {
let conn;
let device;
beforeAll(async () => {
conn = new ewelink({ email, password });
await conn.getCredentials();
});
beforeEach(async () => {
device = await conn.getDevice(thDevice);
});
test('should return current temperature/humidity', async () => {
const { currentTemperature, currentHumidity } = device.params;
const result = await conn.getDeviceCurrentTH(thDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.temperature).toBe(currentTemperature);
expect(result.humidity).toBe(currentHumidity);
});
test('should return current temperature', async () => {
const { currentTemperature } = device.params;
const result = await conn.getDeviceCurrentTemperature(thDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.temperature).toBe(currentTemperature);
});
test('should return current humidity', async () => {
const { currentHumidity } = device.params;
const result = await conn.getDeviceCurrentHumidity(thDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.humidity).toBe(currentHumidity);
});
});
describe('current temperature and humidity: serverless', () => {
let accessToken;
let apiKey;
let connSL;
let device;
beforeAll(async () => {
const conn = new ewelink({ email, password });
const credentials = await conn.getCredentials();
accessToken = credentials.at;
apiKey = credentials.user.apikey;
});
beforeEach(async () => {
connSL = new ewelink({ at: accessToken, apiKey });
device = await connSL.getDevice(thDevice);
});
test('should return current temperature/humidity', async () => {
const { currentTemperature, currentHumidity } = device.params;
const result = await connSL.getDeviceCurrentTH(thDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.temperature).toBe(currentTemperature);
expect(result.humidity).toBe(currentHumidity);
});
test('should return current temperature', async () => {
const { currentTemperature } = device.params;
const result = await connSL.getDeviceCurrentTemperature(thDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.temperature).toBe(currentTemperature);
});
test('should return current humidity', async () => {
const { currentHumidity } = device.params;
const result = await connSL.getDeviceCurrentHumidity(thDevice);
expect(typeof result).toBe('object');
expect(result.status).toBe('ok');
expect(result.humidity).toBe(currentHumidity);
});
});
describe('current temperature and humidity: invalid device', () => {
test('get device current temperature should fail', async () => {
const conn = new ewelink({ email, password });
const temperature = await conn.getDeviceCurrentTemperature('invalid');
expect(typeof temperature).toBe('object');
expect(temperature.msg).toBe(errors['404']);
expect(temperature.error).toBe(404);
});
test('get device current humidity should fail', async () => {
const conn = new ewelink({ email, password });
const humidity = await conn.getDeviceCurrentHumidity('invalid');
expect(typeof humidity).toBe('object');
expect(humidity.msg).toBe(errors['404']);
expect(humidity.error).toBe(404);
});
});
describe('current temperature and humidity: device without sensor', () => {
test('get device current temperature should fail', async () => {
const conn = new ewelink({ email, password });
const temperature = await conn.getDeviceCurrentTemperature(
deviceIdWithoutTempAndHum
);
expect(typeof temperature).toBe('object');
expect(temperature.msg).toBe(errors.noSensor);
expect(temperature.error).toBe(404);
});
test('get device current humidity should fail', async () => {
const conn = new ewelink({ email, password });
const humidity = await conn.getDeviceCurrentHumidity(
deviceIdWithoutTempAndHum
);
expect(typeof humidity).toBe('object');
expect(humidity.msg).toBe(errors.noSensor);
expect(humidity.error).toBe(404);
});
});
describe('current temperature and humidity: invalid credentials', () => {
test('get device current temperature should fail', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const result = await conn.getDeviceCurrentTemperature(thDevice);
expect(typeof result).toBe('object');
expect(result.msg).toBe(errors['406']);
expect(result.error).toBe(406);
});
test('get device current humidity should fail', async () => {
const conn = new ewelink({ email: 'invalid', password: 'credentials' });
const result = await conn.getDeviceCurrentHumidity(thDevice);
expect(typeof result).toBe('object');
expect(result.msg).toBe(errors['406']);
expect(result.error).toBe(406);
});
});