diff --git a/src/jsutils/__tests__/Path-test.ts b/src/jsutils/__tests__/Path-test.ts new file mode 100644 index 0000000000..43bca192c0 --- /dev/null +++ b/src/jsutils/__tests__/Path-test.ts @@ -0,0 +1,36 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +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 = addPath(undefined, 1, 'First'); + const second = addPath(first, 'two', '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 = 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]); + }); +});