From 14a01dfccb637d5cb44bb015407bf24287543c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Negr=C3=B3n?= Date: Tue, 19 Sep 2023 14:02:06 -0400 Subject: [PATCH] implement TypeError check --- .../core/src/tools/create-storekey-tool.js | 7 ++++ .../core/test/tools/create-storekey-tools.js | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 packages/core/test/tools/create-storekey-tools.js diff --git a/packages/core/src/tools/create-storekey-tool.js b/packages/core/src/tools/create-storekey-tool.js index 17240ef0c..372ca03c8 100644 --- a/packages/core/src/tools/create-storekey-tool.js +++ b/packages/core/src/tools/create-storekey-tool.js @@ -14,11 +14,18 @@ const createStoreKeyTool = (input) => { return rpc('get_cursor'); }, + set: (cursor) => { if (!rpc) { return ZapierPromise.reject(new Error('rpc is not available')); } + if (!_.isString(cursor)) { + return ZapierPromise.reject( + new TypeError('cursor value must be a string') + ); + } + return rpc('set_cursor', cursor); }, }; diff --git a/packages/core/test/tools/create-storekey-tools.js b/packages/core/test/tools/create-storekey-tools.js new file mode 100644 index 000000000..ff9123f24 --- /dev/null +++ b/packages/core/test/tools/create-storekey-tools.js @@ -0,0 +1,41 @@ +'use strict'; + +const should = require('should'); + +const { makeRpc, mockRpcCall } = require('./mocky'); +const createStoreKeyTool = require('../../src/tools/create-storekey-tool'); + +describe('storekey (cursor): get, set', () => { + const rpc = makeRpc(); + const cursor = createStoreKeyTool({ _zapier: { rpc } }); + + it('storekey (cursor) get: should return the cursor value given a key', async () => { + const expected = 64; + mockRpcCall(expected); + + const result = await cursor.get('existing-key'); + should(result).eql(expected); + }); + + it('storekey (cursor) set: should raise TypeError on non-string value', async () => { + await should(cursor.set(64)).rejectedWith(TypeError, { + message: 'cursor value must be a string', + }); + + await should(cursor.set(null)).rejectedWith(TypeError, { + message: 'cursor value must be a string', + }); + + await should(cursor.set(undefined)).rejectedWith(TypeError, { + message: 'cursor value must be a string', + }); + }); + + it('storekey (cursor) set: should set a cursor entry', async () => { + const expected = 'ok'; + mockRpcCall(expected); + + const result1 = await cursor.set('test-key'); + should(result1).eql(expected); + }); +});