From 45b9fc20943ee2d6f39ef32fb40539ef70912f74 Mon Sep 17 00:00:00 2001 From: Nicolas Gaborit Date: Thu, 12 Jan 2017 11:45:03 +0100 Subject: [PATCH] Adding more tests for (summary <-> document) transformations (#1665) * Add tests for summaryFromDocument * Add tests for summaryToDocument * Add tests for untitled parts (HR) --- packages/gitbook/src/models/summary.js | 1 - .../summary/__tests__/fixtures/deep.yaml | 121 ++++++++++++ .../__tests__/fixtures/empty-items.yaml | 25 +++ .../summary/__tests__/fixtures/empty.yaml | 6 + .../summary/__tests__/fixtures/parts-ul.yaml | 51 +++++ .../__tests__/fixtures/ul-with-link.yaml | 30 +++ .../summary/__tests__/fixtures/ul.yaml | 25 +++ .../summary/__tests__/readDocument.js | 21 +++ .../modifiers/summary/__tests__/toDocument.js | 168 +++++++++++++++++ .../__tests__/fixtures/summary/deep.yaml | 121 ++++++++++++ .../fixtures/summary/empty-items.yaml | 25 +++ .../__tests__/fixtures/summary/empty.yaml | 6 + .../__tests__/fixtures/summary/parts-ul.yaml | 19 ++ .../parse/__tests__/summaryFromDocument.js | 175 +++++++++++++++--- 14 files changed, 771 insertions(+), 23 deletions(-) create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/deep.yaml create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-items.yaml create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty.yaml create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/parts-ul.yaml create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul-with-link.yaml create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul.yaml create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/readDocument.js create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/toDocument.js create mode 100644 packages/gitbook/src/parse/__tests__/fixtures/summary/deep.yaml create mode 100644 packages/gitbook/src/parse/__tests__/fixtures/summary/empty-items.yaml create mode 100644 packages/gitbook/src/parse/__tests__/fixtures/summary/empty.yaml diff --git a/packages/gitbook/src/models/summary.js b/packages/gitbook/src/models/summary.js index f77b7321d..26db4512d 100644 --- a/packages/gitbook/src/models/summary.js +++ b/packages/gitbook/src/models/summary.js @@ -1,7 +1,6 @@ const is = require('is'); const { List, Record } = require('immutable'); -const error = require('../utils/error'); const LocationUtils = require('../utils/location'); const File = require('./file'); const SummaryPart = require('./summaryPart'); diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/deep.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/deep.yaml new file mode 100644 index 000000000..2b236c0e6 --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/deep.yaml @@ -0,0 +1,121 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + + - kind: block + type: header_two + nodes: + - kind: text + text: '1' + + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.1.1' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.1.2' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.2' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.2.1' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.2.2' + + - kind: block + type: header_two + nodes: + - kind: text + text: '2' + + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1.1.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1.1.1.1' diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-items.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-items.yaml new file mode 100644 index 000000000..5c7ead287 --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-items.yaml @@ -0,0 +1,25 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '' diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty.yaml new file mode 100644 index 000000000..653046563 --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty.yaml @@ -0,0 +1,6 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/parts-ul.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/parts-ul.yaml new file mode 100644 index 000000000..ac2e2a42d --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/parts-ul.yaml @@ -0,0 +1,51 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: Hello + + - kind: block + type: header_two + nodes: + - kind: text + text: Some Part + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: World + + - kind: block + type: hr + nodes: + - kind: text + text: '' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: Yeah diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul-with-link.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul-with-link.yaml new file mode 100644 index 000000000..975fdca6c --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul-with-link.yaml @@ -0,0 +1,30 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: inline + type: link + data: + href: hello.md + nodes: + - kind: text + text: Hello + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: World diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul.yaml new file mode 100644 index 000000000..3af96c9da --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/ul.yaml @@ -0,0 +1,25 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: Hello + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: World diff --git a/packages/gitbook/src/modifiers/summary/__tests__/readDocument.js b/packages/gitbook/src/modifiers/summary/__tests__/readDocument.js new file mode 100644 index 000000000..dfbeebbd8 --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/readDocument.js @@ -0,0 +1,21 @@ +const path = require('path'); +const read = require('read-metadata'); +const { Raw } = require('slate'); + +const FIXTURES = path.resolve(__dirname, 'fixtures'); + +/** + * Read a fixture document from a YAML file. + * @param {String} filename + * @return {Document} + */ +function readDocument(filename) { + filename = path.resolve(FIXTURES, filename); + + const yaml = read.sync(filename); + const document = Raw.deserializeDocument(yaml, { terse: true }); + + return document; +} + +module.exports = readDocument; diff --git a/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js new file mode 100644 index 000000000..93e81246d --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js @@ -0,0 +1,168 @@ +const expect = require('expect'); +const readDocument = require('./readDocument'); +const summaryToDocument = require('../toDocument'); +const Summary = require('../../../models/summary'); +const { Raw } = require('slate'); + +/** + * Expect some parts to be converted to the given document + */ +function expectDocument(documentPath, parts) { + const summary = Summary.createFromParts(parts); + const document = summaryToDocument(summary); + const expectedDocument = readDocument(documentPath); + + expect( + Raw.serializeDocument(document, { terse: true }) + ).toEqual( + Raw.serializeDocument(expectedDocument, { terse: true }) + ); +} + +describe('summaryToDocument', () => { + it('should convert unlinked articles', () => { + expectDocument('ul.yaml', [ + { + title: '', + articles: [ + { + title: 'Hello', + ref: '' + }, + { + title: 'World', + ref: '' + } + ] + } + ]); + }); + + it('should convert articles with links', () => { + expectDocument('ul-with-link.yaml', [ + { + title: '', + articles: [ + { + title: 'Hello', + ref: 'hello.md' + }, + { + title: 'World', + ref: '' + } + ] + } + ]); + }); + + it('should convert parts', () => { + expectDocument('parts-ul.yaml', [ + { + title: '', + articles: [ + { + title: 'Hello', + ref: '' + } + ] + }, + { + title: 'Some Part', + articles: [ + { + title: 'World', + ref: '' + } + ] + }, + { + title: '', // untitled part + articles: [ + { + title: 'Yeah', + ref: '' + } + ] + } + ]); + }); + + it('should convert empty articles', () => { + expectDocument('empty-items.yaml', [ + { + title: '', + articles: [ + { + title: '', + ref: '' + }, + { + title: '', + ref: '' + } + ] + } + ]); + }); + + it('should convert a deep summary', () => { + expectDocument('deep.yaml', [ + { + title: '1', + articles: [ + { + title: '1.1', + articles: [ + { + title: '1.1.1' + }, + { + title: '1.1.2' + } + ] + }, + { + title: '1.2', + articles: [ + { + title: '1.2.1' + }, + { + title: '1.2.2' + } + ] + } + ] + }, + { + title: '2', + articles: [ + { + title: '2.1', + articles: [ + { + title: '2.1.1', + articles: [ + { + title: '2.1.1.1', + articles: [ + { + title: '2.1.1.1.1' + } + ] + } + ] + } + ] + } + ] + } + ]); + }); + + it('should convert an empty summary', () => { + expectDocument('empty.yaml', [ + ]); + }); +}); diff --git a/packages/gitbook/src/parse/__tests__/fixtures/summary/deep.yaml b/packages/gitbook/src/parse/__tests__/fixtures/summary/deep.yaml new file mode 100644 index 000000000..2b236c0e6 --- /dev/null +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/deep.yaml @@ -0,0 +1,121 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + + - kind: block + type: header_two + nodes: + - kind: text + text: '1' + + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.1.1' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.1.2' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.2' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.2.1' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '1.2.2' + + - kind: block + type: header_two + nodes: + - kind: text + text: '2' + + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1.1.1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '2.1.1.1.1' diff --git a/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-items.yaml b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-items.yaml new file mode 100644 index 000000000..5c7ead287 --- /dev/null +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-items.yaml @@ -0,0 +1,25 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '' + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: '' diff --git a/packages/gitbook/src/parse/__tests__/fixtures/summary/empty.yaml b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty.yaml new file mode 100644 index 000000000..653046563 --- /dev/null +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty.yaml @@ -0,0 +1,6 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary diff --git a/packages/gitbook/src/parse/__tests__/fixtures/summary/parts-ul.yaml b/packages/gitbook/src/parse/__tests__/fixtures/summary/parts-ul.yaml index cf19d33f9..d85012b0e 100644 --- a/packages/gitbook/src/parse/__tests__/fixtures/summary/parts-ul.yaml +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/parts-ul.yaml @@ -15,6 +15,7 @@ nodes: nodes: - kind: text text: Hello + - kind: block type: header_two nodes: @@ -31,3 +32,21 @@ nodes: nodes: - kind: text text: World + + - kind: block + type: hr + nodes: + - kind: text + text: '' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: Yeah + diff --git a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js index 7c9436e45..5ec964d81 100644 --- a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js +++ b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js @@ -1,47 +1,178 @@ const expect = require('expect'); const readDocument = require('./utils/readDocument'); const summaryFromDocument = require('../summaryFromDocument'); +const Summary = require('../../models/summary'); function readSummary(filename) { const document = readDocument(filename); return summaryFromDocument(document); } +function expectParts(summary, expectedParts) { + const expectedSummary = Summary.createFromParts(expectedParts); + expect( + summary.toJS().parts + ).toEqual( + expectedSummary.toJS().parts + ); +} + describe('summaryFromDocument', () => { it('should parse from a UL', () => { const summary = readSummary('summary/ul.yaml'); - expect(summary.parts.size).toBe(1); - - const first = summary.getByLevel('1.1'); - expect(first).toExist(); - expect(first.title).toBe('Hello'); - - const last = summary.getByLevel('1.2'); - expect(last).toExist(); - expect(last.title).toBe('World'); + expectParts(summary, [ + { + title: '', + articles: [ + { + title: 'Hello', + ref: '' + }, + { + title: 'World', + ref: '' + } + ] + } + ]); }); it('should parse from a UL with links', () => { const summary = readSummary('summary/ul-with-link.yaml'); - const first = summary.getByLevel('1.1'); - expect(first).toExist(); - expect(first.title).toBe('Hello'); - expect(first.ref).toBe('hello.md'); + expectParts(summary, [ + { + title: '', + articles: [ + { + title: 'Hello', + ref: 'hello.md' + }, + { + title: 'World', + ref: '' + } + ] + } + ]); }); - it('should parse multiple parts', () => { + it('should parse parts', () => { const summary = readSummary('summary/parts-ul.yaml'); - expect(summary.parts.size).toBe(2); - const first = summary.getByLevel('1.1'); - expect(first).toExist(); - expect(first.title).toBe('Hello'); - - const last = summary.getByLevel('2.1'); - expect(last).toExist(); - expect(last.title).toBe('World'); + expectParts(summary, [ + { + title: '', + articles: [ + { + title: 'Hello', + ref: '' + } + ] + }, + { + title: 'Some Part', + articles: [ + { + title: 'World', + ref: '' + } + ] + }, + { + title: '', // untitled part + articles: [ + { + title: 'Yeah', + ref: '' + } + ] + } + ]); }); + it('should parse empty items', () => { + const summary = readSummary('summary/empty-items.yaml'); + + expectParts(summary, [ + { + title: '', + articles: [ + { + title: '', + ref: '' + }, + { + title: '', + ref: '' + } + ] + } + ]); + }); + + it('should parse an deep summary', () => { + const summary = readSummary('summary/deep.yaml'); + + expectParts(summary, [ + { + title: '1', + articles: [ + { + title: '1.1', + articles: [ + { + title: '1.1.1' + }, + { + title: '1.1.2' + } + ] + }, + { + title: '1.2', + articles: [ + { + title: '1.2.1' + }, + { + title: '1.2.2' + } + ] + } + ] + }, + { + title: '2', + articles: [ + { + title: '2.1', + articles: [ + { + title: '2.1.1', + articles: [ + { + title: '2.1.1.1', + articles: [ + { + title: '2.1.1.1.1' + } + ] + } + ] + } + ] + } + ] + } + ]); + }); + + it('should parse an empty summary', () => { + const summary = readSummary('summary/empty.yaml'); + + expectParts(summary, [ + ]); + }); });