From f640727d96865d16ae29b29876704f8bc286f47b Mon Sep 17 00:00:00 2001 From: lana-k Date: Tue, 26 Jan 2021 20:36:56 +0100 Subject: [PATCH] add tests for deserialiseQueries --- tests/unit/storedQueries.spec.js | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/tests/unit/storedQueries.spec.js b/tests/unit/storedQueries.spec.js index 0f4d65d..387d689 100644 --- a/tests/unit/storedQueries.spec.js +++ b/tests/unit/storedQueries.spec.js @@ -6,7 +6,7 @@ describe('storedQueries.js', () => { localStorage.removeItem('myQueries') }) - it('getStoredQueries(empty storage)', () => { + it('getStoredQueries returns emplty array when storage is empty', () => { const queries = storedQueries.getStoredQueries() expect(queries).to.eql([]) }) @@ -97,4 +97,49 @@ describe('storedQueries.js', () => { expect(parsedJson[0].createdAt).to.eql(queryList[0].createdAt) expect(parsedJson[0].chart).to.not.have.property('isPredefined') }) + + it('deserialiseQueries return array for one query', () => { + const str = ` + { + "id": 1, + "name": "foo", + "query": "select * from foo", + "chart": [], + "createdAt": "2020-11-03T14:17:49.524Z" + } + ` + const query = storedQueries.deserialiseQueries(str) + expect(query).to.eql([JSON.parse(str)]) + }) + + it('deserialiseQueries generates new id to avoid duplication', () => { + storedQueries.updateStorage([{id: 1}]) + const str = `[ + { + "id": 1, + "name": "foo", + "query": "select * from foo", + "chart": [], + "createdAt": "2020-11-03T14:17:49.524Z" + }, + { + "id": 2, + "name": "bar", + "query": "select * from bar", + "chart": [], + "createdAt": "2020-11-04T14:17:49.524Z" + } + ]` + + const queries = storedQueries.deserialiseQueries(str) + const parsedStr = JSON.parse(str) + expect(queries[1]).to.eql(parsedStr[1]) + expect(queries[0].id).to.not.equal(parsedStr[0].id) + expect(queries[0]).to.have.property('id') + expect(queries[0].id).to.not.equal(parsedStr[0].id) + expect(queries[0].name).to.equal(parsedStr[0].name) + expect(queries[0].query).to.equal(parsedStr[0].query) + expect(queries[0].chart).to.eql(parsedStr[0].chart) + expect(queries[0].createdAt).to.equal(parsedStr[0].createdAt) + }) })