Skip to content

Commit

Permalink
Merge branch 'master' of github.com:dixieio/zipcelx
Browse files Browse the repository at this point in the history
* 'master' of github.com:dixieio/zipcelx: (25 commits)
  Runned new build
  Removed unused constant
  Fixed linting errors
  Added Contributing file
  Updated tests to use a baseConfig object, to run tests from
  Changed test copy
  Changed the validation check to just throw one validation error, added test to ensure error gets called in validation fails for the tool
  Updated validator just to throw console.errors and added tests
  Renamed file, and Updated tests to not run assert
  Added test for formatRow
  Removed reuse of xml string, added expected xml strings to formatCell test and added Cell type of number test
  Updated test date, made test to ensure it generates string cell type
  Added test for formatCell type catch and fallback, and moved warning to constants
  Added test for cell of type String formatter
  Added test for cell of type Number formatter
  added test for the generator cell number
  Moved error messages to constants
  Added test & validator to ensure sheet data is an array
  Added test to ensure data contians array, updated tests to use baseConfig
  Updated child validator
  ...
  • Loading branch information
Nopzen committed Feb 27, 2017
2 parents d0905f4 + bcf3f45 commit 5d41e42
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 53 deletions.
65 changes: 65 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
We 💜 contributions
-------------------

While we love contributions, we also need to ensure that our library is of great quality. Thus we require you to follow some simple guidelines when you're submitting your contributions.

## Reporting Issues and Asking Questions

Before opening an issue, please search the [issue tracker](https://github.com/dixieio/redux-json-api/issues) to make sure your issue hasn’t already been reported.

## Development

Visit the [issue tracker](https://github.com/dixieio/redux-json-api/issues) to find a list of open issues that need attention.

Fork, then clone the repo:

```
git clone https://github.com/your-username/redux-json-api.git
```

### Testing

To run tests:

```
npm run test // Will run tests once
```

To continuously watch and run tests, run the following:

```
npm run test:watch
```

### Linting

To Lint
```
npm run lint
```

### Building

To build run:

```
npm run build
```

### Quality insurance
The `build` command should handle it but always run the `lint` command to ensure we are all developing with the same code standards.

### Submitting a Pull Request

For non-trivial changes, please open an issue with a proposal for a new feature or refactoring before starting on the work. We don’t want you to waste your efforts on a pull request that we won’t want to accept.

On the other hand, sometimes the best way to start a conversation *is* to send a pull request. Use your best judgement!

1. Open a new issue in the [Issue tracker](https://github.com/dixieio/redux-json-api/issues)
1. Fork the repo
1. Create a new feature branch based off the `master` branch
1. Create breaking test(s) before implementing any fixes or functionality
1. Make your changes
1. Submit a pull request, referencing any issue that it resolves

Thank you, we 💜 your contributions!
18 changes: 18 additions & 0 deletions __test__/baseConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
Basic working config
*/

export default {
filename: 'report',
sheet: {
data: [
[{
value: 'Test',
type: 'string'
}, {
value: 1000,
type: 'number'
}]
]
}
};
7 changes: 7 additions & 0 deletions __test__/commons/generatorCellNumber.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import generatorCellNumber from '../../src/commons/generatorCellNumber';

describe('Cell number generator', () => {
it('should create cell number A1', () => {
expect(generatorCellNumber(0, 1)).toBe('A1');
});
});
31 changes: 31 additions & 0 deletions __test__/formatters/cells/formatCell.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { WARNING_INVALID_TYPE } from '../../../src/commons/constants';
import formatCell from '../../../src/formatters/cells/formatCell';
import baseConfig from '../../baseConfig';

const cells = baseConfig.sheet.data[0];

console.warn = jest.genMockFn();

describe('Format Cell', () => {
describe('Create a cell of type sting', () => {
const expectedXML = '<c r="A1" t="inlineStr"><is><t>Test</t></is></c>';

it('Should fallback to string if invalid type was supplied', () => {
const cell = Object.assign({}, cells[0], { type: 'date' });
formatCell(cell);
expect(console.warn).toBeCalledWith(WARNING_INVALID_TYPE);
expect(formatCell(cell, 0, 1)).toBe(expectedXML);
});

it('Create cell', () => {
expect(formatCell(cells[0], 0, 1)).toBe(expectedXML);
});
});

describe('Create a cell of type number', () => {
const expectedXML = '<c r="B1"><v>1000</v></c>';
it('Create cell', () => {
expect(formatCell(cells[1], 1, 1)).toBe(expectedXML);
});
});
});
9 changes: 9 additions & 0 deletions __test__/formatters/cells/generatorNumberCell.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import generatorNumberCell from '../../../src/formatters/cells/generatorNumberCell';

export const expectedXML = '<c r="A1"><v>1000</v></c>';

describe('Cell of type Number', () => {
it('Should create a new xml markup cell', () => {
expect(generatorNumberCell(0, 1000, 1)).toBe(expectedXML);
});
});
9 changes: 9 additions & 0 deletions __test__/formatters/cells/generatorStringCell.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import generatorStringCell from '../../../src/formatters/cells/generatorStringCell';

const expectedXML = '<c r="A1" t="inlineStr"><is><t>Test</t></is></c>';

describe('Cell of type String', () => {
it('Should create a new xml markup cell', () => {
expect(generatorStringCell(0, 'Test', 1)).toBe(expectedXML);
});
});
10 changes: 10 additions & 0 deletions __test__/formatters/rows/formatRows.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import formatRow from '../../../src/formatters/rows/formatRow';
import baseConfig from '../../baseConfig';

const expectedXML = '<row r="1"><c r="A1" t="inlineStr"><is><t>Test</t></is></c><c r="B1"><v>1000</v></c></row>';

describe('Format Row', () => {
it('Should create one row from given data', () => {
expect(formatRow(baseConfig.sheet.data[0], 0)).toBe(expectedXML);
});
});
69 changes: 54 additions & 15 deletions __test__/validator.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
import validator from '../src/validator';
import {
MISSING_KEY_FILENAME,
INVALID_TYPE_FILENAME,
INVALID_TYPE_SHEET,
INVALID_TYPE_SHEET_DATA
} from '../src/commons/constants';
import baseConfig from './baseConfig';

const data = [
[{
value: 'Monkey',
type: 'string'
}, {
value: 1000,
type: 'number'
}]
];

describe('Data validator', () => {
it('Should ensure that Data passed is an Array', () => {
expect(validator(data)).toBe(true);
const configDescription = expect.objectContaining({
filename: expect.any(String),
sheet: expect.objectContaining({
data: expect.arrayContaining(baseConfig.sheet.data)
})
});
const errorObjectDescription = expect.objectContaining({
error: expect.any(String),
});

console.error = jest.genMockFn();

describe('Validator', () => {
it('Should ensure that being called with correct config', () => {
expect(baseConfig).toEqual(configDescription);
});
it('Should ensure that argument Data\'s childs is arrays', () => {
expect(validator(data)).toBe(true);

it('If validation is successfull return true', () => {
expect(validator(baseConfig)).toBe(true);
});

it('If validation fails it should call console.error', () => {
let config = Object.assign({}, baseConfig, {filename: 1234});
validator(config)
expect(console.error).toBeCalled();
});

describe('Filename Validator', () => {
it('Should be a property of the config', () => {
let config = Object.assign({}, config);
delete config.filename;
expect(validator(config)).toBe(false);
expect(console.error).toBeCalledWith(MISSING_KEY_FILENAME);
});
});

describe('Sheet data', () => {
it('Should ensure that sheet data key is an array', () => {
let config = Object.assign({}, baseConfig, { sheet: { data: { test: 'test'} } });
expect(validator(config)).toBe(false);
expect(console.error).toBeCalledWith(INVALID_TYPE_SHEET);
});

it('Should ensure each of the childs is an array', () => {
let config = Object.assign({}, baseConfig, { sheet: { data: [{test: 'demo'}] } });
expect(validator(config)).toBe(false);
expect(console.error).toBeCalledWith(INVALID_TYPE_SHEET_DATA);
});
})
});
32 changes: 15 additions & 17 deletions __test__/xmlFormatters.spec.js → __test__/zipcelx.spec.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import assert from 'assert';
import generatorRows from '../src/formatters/rows/generatorRows';
import { generateXMLWorksheet } from '../src/zipcelx';
import zipcelx, { generateXMLWorksheet } from '../src/zipcelx';
import baseConfig from './baseConfig';

const data = [
[{
value: 'Monkey',
type: 'string'
}, {
value: 1000,
type: 'number'
}]
];
console.error = jest.genMockFn();

describe('XML Formatter', () => {
const rowsXML = `<row r="1"><c r="A1" t="inlineStr"><is><t>Monkey</t></is></c><c r="B1"><v>1000</v></c></row>`;
describe('Zipcelx', () => {
const rowsXML = `<row r="1"><c r="A1" t="inlineStr"><is><t>Test</t></is></c><c r="B1"><v>1000</v></c></row>`;

it('Should console log error if validator fails', () => {
let config = Object.assign({}, baseConfig, { sheet: { data: [{test: 'demo'}] } });
zipcelx(config);
expect(console.error).toBeCalled();
});

it('Should map row arrays to XML markup', () => {
assert.strictEqual(generatorRows(data), rowsXML);
})
expect(generatorRows(baseConfig.sheet.data)).toBe(rowsXML);
});

it('Should generate and XML Worksheet from template', () => {
const expectedXML = [
'<?xml version="1.0" ?>',
`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main"><sheetData>${rowsXML}</sheetData></worksheet>`
].join('\n');
assert.strictEqual(generateXMLWorksheet(data), expectedXML);
})
expect(generateXMLWorksheet(baseConfig.sheet.data)).toBe(expectedXML);
});
});
9 changes: 8 additions & 1 deletion lib/commons/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ Object.defineProperty(exports, "__esModule", {
});
var CELL_TYPE_STRING = exports.CELL_TYPE_STRING = 'string';
var CELL_TYPE_NUMBER = exports.CELL_TYPE_NUMBER = 'number';
var validTypes = exports.validTypes = [CELL_TYPE_STRING, CELL_TYPE_NUMBER];
var validTypes = exports.validTypes = [CELL_TYPE_STRING, CELL_TYPE_NUMBER];

var MISSING_KEY_FILENAME = exports.MISSING_KEY_FILENAME = 'Zipclex config missing property filename';
var INVALID_TYPE_FILENAME = exports.INVALID_TYPE_FILENAME = 'Zipclex filename can only be of type string';
var INVALID_TYPE_SHEET = exports.INVALID_TYPE_SHEET = 'Zipcelx sheet data is not of type array';
var INVALID_TYPE_SHEET_DATA = exports.INVALID_TYPE_SHEET_DATA = 'Zipclex sheet data childs is not of type array';

var WARNING_INVALID_TYPE = exports.WARNING_INVALID_TYPE = 'Invalid type supplied in cell config, falling back to "string"';
2 changes: 1 addition & 1 deletion lib/formatters/cells/formatCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

exports.default = function (cell, index, rowIndex) {
if (_constants.validTypes.indexOf(cell.type) === -1) {
console.warn('Invalid type supplied in cell config, falling back to "string"');
console.warn(_constants.WARNING_INVALID_TYPE);
cell.type = _constants.CELL_TYPE_STRING;
}

Expand Down
29 changes: 22 additions & 7 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
"use strict";
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _constants = require('./commons/constants');

var childValidator = function childValidator(array) {
return array.reduce(function (bool, item) {
return Array.isArray(item) && bool;
}, true);
return array.every(function (item) {
return Array.isArray(item);
});
};

exports.default = function (data) {
if (!Array.isArray(data)) {
exports.default = function (config) {
if (!config.filename) {
console.error(_constants.MISSING_KEY_FILENAME);
return false;
}

if (typeof config.filename !== 'string') {
console.error(_constants.INVALID_TYPE_FILENAME);
return false;
}

if (!Array.isArray(config.sheet.data)) {
console.error(_constants.INVALID_TYPE_SHEET);
return false;
}

if (!childValidator(data)) {
if (!childValidator(config.sheet.data)) {
console.error(_constants.INVALID_TYPE_SHEET_DATA);
return false;
}

Expand Down
1 change: 0 additions & 1 deletion lib/zipcelx.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ var generateXMLWorksheet = exports.generateXMLWorksheet = function generateXMLWo

exports.default = function (config) {
if (!(0, _validator2.default)(config.sheet.data)) {
console.error('Invalid data format, see: https://github.com/dixieio/zipcelx/issues/1 for supported format.');
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/commons/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export const CELL_TYPE_STRING = 'string';
export const CELL_TYPE_NUMBER = 'number';
export const validTypes = [CELL_TYPE_STRING, CELL_TYPE_NUMBER];

export const MISSING_KEY_FILENAME = 'Zipclex config missing property filename';
export const INVALID_TYPE_FILENAME = 'Zipclex filename can only be of type string';
export const INVALID_TYPE_SHEET = 'Zipcelx sheet data is not of type array';
export const INVALID_TYPE_SHEET_DATA = 'Zipclex sheet data childs is not of type array';

export const WARNING_INVALID_TYPE = 'Invalid type supplied in cell config, falling back to "string"';
4 changes: 2 additions & 2 deletions src/formatters/cells/formatCell.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { validTypes, CELL_TYPE_STRING } from '../../commons/constants';
import { validTypes, CELL_TYPE_STRING, WARNING_INVALID_TYPE } from '../../commons/constants';
import generatorStringCell from './generatorStringCell';
import generatorNumberCell from './generatorNumberCell';

export default (cell, index, rowIndex) => {
if (validTypes.indexOf(cell.type) === -1) {
console.warn('Invalid type supplied in cell config, falling back to "string"');
console.warn(WARNING_INVALID_TYPE);
cell.type = CELL_TYPE_STRING;
}

Expand Down
Loading

0 comments on commit 5d41e42

Please sign in to comment.