From be6212c26ff04c1264de64b391b759164fe2a4e4 Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Fri, 6 Aug 2021 10:38:36 +0200 Subject: [PATCH 1/2] Add tests for Path function --- src/jsutils/__tests__/Path-test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/jsutils/__tests__/Path-test.ts diff --git a/src/jsutils/__tests__/Path-test.ts b/src/jsutils/__tests__/Path-test.ts new file mode 100644 index 0000000000..05e7e72ff6 --- /dev/null +++ b/src/jsutils/__tests__/Path-test.ts @@ -0,0 +1,24 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +import type { Path } from '../Path'; +import { addPath, pathToArray } from '../Path'; + +describe('Path', () => { + it('can add a new key to an existing Path', () => { + const first: Path = { prev: undefined, key: 1, typename: 'First' }; + const second = addPath(first, 'two', 'Second'); + expect(second.prev).to.equal(first); + expect(second.key).to.equal('two'); + expect(second.typename).to.equal('Second'); + }); + + it('can convert a Path to an array of its keys', () => { + const root: Path = { prev: undefined, key: 0, typename: 'Root' }; + const first: Path = { prev: root, key: 'one', typename: 'First' }; + const second: Path = { prev: first, key: 2, typename: 'Second' }; + expect(pathToArray(second)) + .to.be.an('array') + .that.deep.equals([0, 'one', 2]); + }); +}); From 04d5cc3454086d9aa89eed871594d31cde701958 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Tue, 19 Oct 2021 10:50:46 +0300 Subject: [PATCH 2/2] Review changes --- src/jsutils/__tests__/Path-test.ts | 34 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/jsutils/__tests__/Path-test.ts b/src/jsutils/__tests__/Path-test.ts index 05e7e72ff6..43bca192c0 100644 --- a/src/jsutils/__tests__/Path-test.ts +++ b/src/jsutils/__tests__/Path-test.ts @@ -1,24 +1,36 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; -import type { Path } from '../Path'; import { addPath, pathToArray } from '../Path'; describe('Path', () => { + it('can create a Path', () => { + const first = addPath(undefined, 1, 'First'); + + expect(first).to.deep.equal({ + prev: undefined, + key: 1, + typename: 'First', + }); + }); + it('can add a new key to an existing Path', () => { - const first: Path = { prev: undefined, key: 1, typename: 'First' }; + const first = addPath(undefined, 1, 'First'); const second = addPath(first, 'two', 'Second'); - expect(second.prev).to.equal(first); - expect(second.key).to.equal('two'); - expect(second.typename).to.equal('Second'); + + expect(second).to.deep.equal({ + prev: first, + key: 'two', + typename: 'Second', + }); }); it('can convert a Path to an array of its keys', () => { - const root: Path = { prev: undefined, key: 0, typename: 'Root' }; - const first: Path = { prev: root, key: 'one', typename: 'First' }; - const second: Path = { prev: first, key: 2, typename: 'Second' }; - expect(pathToArray(second)) - .to.be.an('array') - .that.deep.equals([0, 'one', 2]); + const root = addPath(undefined, 0, 'Root'); + const first = addPath(root, 'one', 'First'); + const second = addPath(first, 2, 'Second'); + + const path = pathToArray(second); + expect(path).to.deep.equal([0, 'one', 2]); }); });