diff options
author | Nicolas Gaborit <soreine.plume@gmail.com> | 2017-01-12 11:45:03 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2017-01-12 11:45:03 +0100 |
commit | 45b9fc20943ee2d6f39ef32fb40539ef70912f74 (patch) | |
tree | 9e76a3e7579fbd69bc80eeeb16716e6240634ea2 | |
parent | a96ef569bdc9d48ff344c19e9df39e2c4560ff68 (diff) | |
download | gitbook-45b9fc20943ee2d6f39ef32fb40539ef70912f74.zip gitbook-45b9fc20943ee2d6f39ef32fb40539ef70912f74.tar.gz gitbook-45b9fc20943ee2d6f39ef32fb40539ef70912f74.tar.bz2 |
Adding more tests for (summary <-> document) transformations (#1665)
* Add tests for summaryFromDocument
* Add tests for summaryToDocument
* Add tests for untitled parts (HR)
14 files changed, 770 insertions, 22 deletions
diff --git a/packages/gitbook/src/models/summary.js b/packages/gitbook/src/models/summary.js index f77b732..26db451 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 0000000..2b236c0 --- /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 0000000..5c7ead2 --- /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 0000000..6530465 --- /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 0000000..ac2e2a4 --- /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 0000000..975fdca --- /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 0000000..3af96c9 --- /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 0000000..dfbeebb --- /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 0000000..93e8124 --- /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 0000000..2b236c0 --- /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 0000000..5c7ead2 --- /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 0000000..6530465 --- /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 cf19d33..d85012b 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 7c9436e..5ec964d 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'); + 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'); - const last = summary.getByLevel('2.1'); - expect(last).toExist(); - expect(last.title).toBe('World'); + 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, [ + ]); + }); }); |