Skip to content

Commit

Permalink
Merge pull request #3 from DFE-Digital/feature/complexity-refactors
Browse files Browse the repository at this point in the history
♻️Complexity Refactors
  • Loading branch information
kylewelsby authored Mar 6, 2020
2 parents 6fdf007 + 318db70 commit 298dfa1
Show file tree
Hide file tree
Showing 26 changed files with 3,480 additions and 674 deletions.
5 changes: 5 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins:
eslint:
enabled: true
config:
config: ./eslintrc.js
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lib
node_modules
coverage
out
docs
20 changes: 14 additions & 6 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ module.exports = {
root: true,
env: {
es6: true,
node: true
node: true,
},
parser: "babel-eslint",
extends: ["eslint:recommended", "plugin:prettier/recommended"],
parser: 'babel-eslint',
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
rules: {
"prettier/prettier": "off"
}
};
semi: ['error', 'always'],
'prettier/prettier': ['error', { sindleQuote: true }],
'max-lines-per-function': ['error', 25],
'no-console': [
'error',
{
allow: ['error'],
},
],
},
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ coverage
*DS_Store
__tests__/__fixtures__/output.odp
lib
*.tgz
*.tgz
docs
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
trailingComma: "es5"
trailingComma: "none"
tabWidth: 2
semi: true
singleQuote: true
Expand Down
5 changes: 4 additions & 1 deletion __tests__/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
],
"extends": [
"plugin:jest/recommended"
]
],
"rules": {
"max-lines-per-function": "off"
}
}
119 changes: 67 additions & 52 deletions __tests__/Document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ async function readFixture(filename) {
const masterStylesFile = files.find(f => f.path === 'styles.xml');
const contentFile = files.find(f => f.path === 'content.xml');
const presentation = new Presentation(JSON.parse(toJson(contentFile.data)));
const style = new Style(
JSON.parse(toJson(masterStylesFile.data)),
presentation
);
const style = new Style(JSON.parse(toJson(masterStylesFile.data)));
const subject = new Document();
return {
presentation,
style,
subject,
subject
};
}

Expand All @@ -45,6 +42,32 @@ describe('Document', () => {
subject = obj.subject;
});

describe('.manifestFiles', () => {
it('has a initial set of manifest files that will exist', () => {
expect(subject.manifestFiles).toContainEqual(
expect.objectContaining({
mimeType: 'application/vnd.oasis.opendocument.presentation',
path: '/',
version: '1.2'
})
);

expect(subject.manifestFiles).toContainEqual(
expect.objectContaining({
mimeType: 'text/xml',
path: 'content.xml'
})
);

expect(subject.manifestFiles).toContainEqual(
expect.objectContaining({
mimeType: 'text/xml',
path: 'styles.xml'
})
);
});
});

describe('.mergeFile', () => {
let file = path.join(__dirname, '__fixtures__/out1.odp');
afterEach(() => {
Expand Down Expand Up @@ -82,34 +105,14 @@ describe('Document', () => {
});
});

describe('.merge', () => {
describe('.mergeContent', () => {
it('merges multiple presentations', () => {
let doc = new Document();
let fixtures = [fixture1, fixture2];
let presentations = [];

fixtures.forEach(fixture => {
presentations.push(new Presentation(fixture));
});
let subject = new Document();

presentations.forEach(pres => {
doc.merge(pres);
});

let slides = get(
doc.doc,
'office:document-content.office:body.office:presentation.draw:page'
);
subject.mergeContent(new Presentation(fixture1, 0));
subject.mergeContent(new Presentation(fixture2, 1));

fixtures.forEach(fixture => {
let fixtureSlides = get(
fixture,
'office:document-content.office:body.office:presentation.draw:page'
);
fixtureSlides.forEach(slide => {
expect(slides).toContainEqual(slide);
});
});
expect(subject.doc).toMatchSnapshot();
});
});

Expand All @@ -134,34 +137,46 @@ describe('Document', () => {
expect(subject.stylesDoc).toMatchSnapshot();
});

describe('master-page', () => {
let actual;
beforeEach(() => {
let key =
'office:document-styles.office:master-styles.style:master-page';
actual = get(subject.stylesDoc, key);
});
[
'office:document-styles.office:master-styles.style:master-page',
'office:document-styles.office:automatic-styles.style:style'
].forEach(key => {
describe(key, () => {
let actual;
beforeEach(() => {
actual = get(subject.stylesDoc, key);
});

it('contains the master style names', () => {
let actualNames = actual.map(i => i['style:name']);
expect(actualNames).toMatchSnapshot();
it(`contains the expected contents of ${key}`, () => {
expect(actual).toMatchSnapshot();
});
});
});
});

it('contains the master frames', () => {
let actualNames = actual.map(i => i['draw:frame']);
expect(actualNames).toMatchSnapshot();
});
describe('pipe', () => {
let tmpfile = './tmpfile';
beforeEach(() => {
subject = new Document();
});
afterEach(() => {
fs.unlinkSync(tmpfile);
});

describe('office:automatic-styles.style:style', () => {
let actual;
beforeEach(() => {
let key = 'office:document-styles.office:automatic-styles.style:style';
actual = get(subject.stylesDoc, key);
it('handles error', async () => {
expect.assertions(2);
let stream = fs.createWriteStream(tmpfile);
stream.destroy();
subject.on('error', err => {
expect(err).toMatchInlineSnapshot(
`[Error: Cannot call write after a stream was destroyed]`
);
});

it('contains the automatic-styles from both files', () => {
expect(actual).toMatchSnapshot();
return subject.pipe(stream).catch(e => {
expect(e).toMatchInlineSnapshot(
Error,
`[Error: Cannot call write after a stream was destroyed]`
);
});
});
});
Expand Down
24 changes: 11 additions & 13 deletions __tests__/Presentation.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import fs from 'fs';
import path from 'path';
import Presenation from '../src/Presentation';
import Slide from '../src/Slide';

const fixture = JSON.parse(
fs.readFileSync(path.join(__dirname, "__fixtures__/example.json"))
fs.readFileSync(path.join(__dirname, '__fixtures__/example.json'))
);

describe("Presentation", () => {
describe('.slides', () => {
it('creates an array of slides', () => {
const presenation = new Presenation(fixture)
let actual = presenation.slides

expect(actual).toBeInstanceOf(Array)
expect(actual[0]).toBeInstanceOf(Slide)
})
})
})
describe('Presentation', () => {
describe('.uniqueStyleIDs()', () => {
it('renames style names with a prefixed ID', () => {
let subject = new Presenation(fixture, 0);
expect(JSON.stringify(subject.data)).toContain(
'"text:style-name":"0-a1219"'
);
});
});
});
65 changes: 0 additions & 65 deletions __tests__/Slide.test.js

This file was deleted.

28 changes: 2 additions & 26 deletions __tests__/Style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,20 @@ import path from 'path';
import decompress from 'decompress';
import { toJson } from 'xml2json';
import Style from '../src/Style';
import Presentation from '../src/Presentation';

describe('Style', () => {
let subject;
let presentation;
beforeEach(async () => {
const files = await decompress(
path.join(__dirname, '__fixtures__/file1.odp')
);
const masterStylesFile = files.find(f => f.path === 'styles.xml');
const contentFile = files.find(f => f.path === 'content.xml');
presentation = new Presentation(JSON.parse(toJson(contentFile.data)));
subject = new Style(
JSON.parse(toJson(masterStylesFile.data)),
presentation
);
});
describe('masterPages', () => {
it('returns master styles for the presenation', () => {
expect(subject.masterPages).toMatchSnapshot();
});
});

describe('presentationPageLayouts', () => {
it('returns layouts for the presentation', () => {
expect(subject.presentationPageLayouts).toMatchSnapshot();
});
subject = new Style(JSON.parse(toJson(masterStylesFile.data)));
});

describe('namespaces', () => {
it('returns the style XML namespaces for this presentation', () => {
expect(subject.namespaces).toMatchSnapshot()
});
});

describe('styles', () => {
it.skip('returns styles for the presentation slides', () => {
expect(subject.styles).toMatchSnapshot();
expect(subject.namespaces).toMatchSnapshot();
});
});
});
Loading

0 comments on commit 298dfa1

Please sign in to comment.