From 29b93e6cba29915683acb503041c1d4eddbe0f83 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Thu, 11 Jan 2018 00:08:38 -0600 Subject: [PATCH] This adds a missing test for when a request is already in a cache. --- .../__snapshots__/requestList.spec.js.snap | 21 +++++++++++++++++++ src/__tests__/requestList.spec.js | 13 +++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/__snapshots__/requestList.spec.js.snap diff --git a/src/__tests__/__snapshots__/requestList.spec.js.snap b/src/__tests__/__snapshots__/requestList.spec.js.snap new file mode 100644 index 0000000..74a03f5 --- /dev/null +++ b/src/__tests__/__snapshots__/requestList.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`RequestList should execute requests by alias. 1`] = ` +Object { + "body": "{\\"hello\\": \\"world\\"}", + "changed": true, + "request": Object { + "body": Object { + "lastname": "Diaz", + "name": "Sergio", + }, + "endpoint": "http://martianwabbit.com/user", + "headers": Object {}, + }, + "response": Object { + "body": "{\\"hello\\": \\"world\\"}", + "headers": Array [], + "status": 200, + }, +} +`; diff --git a/src/__tests__/requestList.spec.js b/src/__tests__/requestList.spec.js index 9087c59..81e2e61 100644 --- a/src/__tests__/requestList.spec.js +++ b/src/__tests__/requestList.spec.js @@ -3,6 +3,10 @@ const requestPromiseNativeMock = require('request-promise-native'); describe('RequestList', () => { const endpoint = 'http://martianwabbit.com'; + let env = { + environmental: true + }; + const doc = { 'POST /session': null, 'Not a Request': null, @@ -21,6 +25,7 @@ describe('RequestList', () => { requestPromiseNativeMock.fail = false; requests = new RequestList(doc, { ENDPOINT: endpoint, + ENVIRONMENT: env, PLUGINS: [ { 'beau-jwt': { @@ -54,7 +59,7 @@ describe('RequestList', () => { }); it('should execute requests by alias.', async () => { - await requests.execByAlias('user'); + await expect(requests.execByAlias('user')).resolves.toMatchSnapshot(); }); it('should fail if the request fails', async () => { @@ -62,6 +67,12 @@ describe('RequestList', () => { await expect(requests.execByAlias('user')).rejects.toThrow(Error); }); + it('should return a cached result if available', async () => { + const obj = { test: true }; + requests.cache.add('$test', obj); + await expect(requests.execByAlias('test')).resolves.toBe(obj); + }); + it('should fail if the alias is not found', async () => { await expect(requests.execByAlias('notAnAlias')).rejects.toThrow(Error); });