From a14a78383043fc021dba46a43afeacdee6b092c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marti=CC=81n=20M?= Date: Fri, 28 Jun 2019 01:42:23 -0300 Subject: [PATCH] added test cases --- test/_expectations.js | 28 ++++++++++++ test/credentials.json | 5 +++ test/main.spec.js | 99 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 test/_expectations.js create mode 100644 test/credentials.json create mode 100644 test/main.spec.js diff --git a/test/_expectations.js b/test/_expectations.js new file mode 100644 index 0000000..91f778f --- /dev/null +++ b/test/_expectations.js @@ -0,0 +1,28 @@ +const loginExpectations = { + at: expect.any(String), + user: { email: expect.any(String) }, + region: expect.any(String), +}; + +const allDevicesExpectations = { + name: expect.any(String), + deviceid: expect.any(String), + apikey: expect.any(String), + params: expect.any(Object), + showBrand: expect.any(Boolean), + uiid: expect.any(Number), +}; + +const specificDeviceExpectations = { + apikey: expect.any(String), + deviceid: expect.any(String), + name: expect.any(String), + online: expect.any(Boolean), + uiid: expect.any(Number), +}; + +module.exports = { + loginExpectations, + allDevicesExpectations, + specificDeviceExpectations, +}; diff --git a/test/credentials.json b/test/credentials.json new file mode 100644 index 0000000..83d158c --- /dev/null +++ b/test/credentials.json @@ -0,0 +1,5 @@ +{ + "email": "", + "password": "", + "deviceId": "" +} \ No newline at end of file diff --git a/test/main.spec.js b/test/main.spec.js new file mode 100644 index 0000000..b99149b --- /dev/null +++ b/test/main.spec.js @@ -0,0 +1,99 @@ +const ewelink = require('../main'); +const { email, password, deviceId } = require('./credentials.json'); +const { + loginExpectations, + allDevicesExpectations, + specificDeviceExpectations, +} = require('./_expectations'); + +describe('env: node script', () => { + let conn; + + beforeAll(() => { + conn = new ewelink({ email, password }); + }); + + 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(deviceId); + expect(typeof device).toBe('object'); + expect(device.deviceid).toBe(deviceId); + expect(device).toMatchObject(specificDeviceExpectations); + }); +}); + +describe('env: serverless', () => { + let accessToken; + + test('login into ewelink', async () => { + const conn = new ewelink({ email, password }); + const login = await conn.login(); + accessToken = login.at; + expect(typeof login).toBe('object'); + expect(login).toMatchObject(loginExpectations); + }); + + test('get all devices', async () => { + const conn = new ewelink({ at: accessToken }); + const devices = await conn.getDevices(); + expect(Array.isArray(devices)).toBe(true); + expect(devices[0]).toMatchObject(allDevicesExpectations); + }); + + test('get specific device', async () => { + const conn = new ewelink({ at: accessToken }); + const device = await conn.getDevice(deviceId); + expect(typeof device).toBe('object'); + expect(device.deviceid).toBe(deviceId); + expect(device).toMatchObject(specificDeviceExpectations); + }); +}); + +describe('invalid credentials', () => { + test('no credentials given', async () => { + const conn = new ewelink({}); + expect(typeof conn).toBe('object'); + expect(conn.error).toBe('No credentials provided'); + }); + + test('get error response on api login', async () => { + const conn = new ewelink({ email: 'invalid', password: 'credentials' }); + const login = await conn.login(); + expect(typeof login).toBe('object'); + expect(login).toMatchObject({ + error: expect.any(Number), + }); + expect(login.error).toBe(400); + }); + + test('get error response on all devices', async () => { + const conn = new ewelink({ email: 'invalid', password: 'credentials' }); + const devices = await conn.getDevices(); + expect(typeof devices).toBe('object'); + expect(devices).toMatchObject({ + msg: expect.any(String), + }); + expect(devices.error).toBe(401); + }); + + test('get error response on specific device', async () => { + const conn = new ewelink({ email: 'invalid', password: 'credentials' }); + const device = await conn.getDevice('invalid device id'); + expect(typeof device).toBe('object'); + expect(device).toMatchObject({ + msg: expect.any(String), + }); + expect(device.error).toBe(401); + }); +});