Adding more tests for (summary <-> document) transformations (#1665)

* Add tests for summaryFromDocument

* Add tests for summaryToDocument

* Add tests for untitled parts (HR)
This commit is contained in:
Nicolas Gaborit
2017-01-12 11:45:03 +01:00
committed by Samy Pessé
parent a96ef569bd
commit 45b9fc2094
14 changed files with 771 additions and 23 deletions
-1
View File
@@ -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');
@@ -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'
@@ -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: ''
@@ -0,0 +1,6 @@
nodes:
- kind: block
type: header_one
nodes:
- kind: text
text: Summary
@@ -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
@@ -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
@@ -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
@@ -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;
@@ -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', [
]);
});
});
@@ -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'
@@ -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: ''
@@ -0,0 +1,6 @@
nodes:
- kind: block
type: header_one
nodes:
- kind: text
text: Summary
@@ -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
@@ -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, [
]);
});
});