Skip to content

Commit

Permalink
Merge pull request #11 from Siemienik/issue/3-tests
Browse files Browse the repository at this point in the history
#3 tests (#6 and examples)
  • Loading branch information
Siemienik authored Mar 26, 2020
2 parents 56a6314 + d105cb8 commit 9bd579a
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 18 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ It consumes template which is common Excel file, then add yours data (called Vie
npm i xlsx-renderer --save
```

2. TODO add some example here (https://github.com/Siemienik/xlsx-renderer/issues/6)

## Sample code:

```javascript
Expand Down Expand Up @@ -50,12 +48,12 @@ for more example I invite to tests data: [click here and check `Renderer` folder
| Worksheet<br/>Navigation<br/>Loop | [FinishCell](./src/cell/FinishCell.ts) | 7 | `#! FINISH conditionPath` | Finish rendering for current worksheet and: <br/> 1) go to next worksheet if `conditionPath===true`<br/> 2) repeat this template worksheet again (`conditionPath === false`) - looping through worksheets <br/> 3) finished whole rendering when this worksheet is the last one. | **Examples:**<br/> `#! FINISHED ` or `#! FINISHED itemFromLoop.__iterated` |
| Worksheet | [WsNameCell](./src/cell/WsNameCell.ts) | 13 | `#! WS_NAME pathToVariable` | Set worksheet's name. | **Examples:** <br/> `#! WS_NAME worksheetName` <br/> `#! WS_NAME item.title` <br/> `#! WS_NAME translatedNames.0` |
| Loop | **TODO: describe it!** [DumpColsCell](./src/cell/DumpColsCell.ts) | | | | |
| Loop | **TODO: describe it! test done: simple; tests todo: nested, with formula, stripped, through worksheets,** [ForEachCell](./src/cell/ForEachCell.ts) | | | | |
| Loop | **TODO: describe it!** [ContinueCell](./src/cell/ContinueCell.ts) | | | | |
| Loop | **TODO: describe it!** [EndLoopCell](./src/cell/EndLoopCell.ts) | | | | |
| Aggregation | **TODO: describe it!** [AverageCell](./src/cell/AverageCell.ts) | | | | |
| Aggregation| **TODO: describe it!** [SumCell](./src/cell/SumCell.ts) | | | | |
| View Model | **TODO: test - nested loop** [DeleteCell](./src/cell/DeleteCell.ts) | 14 | `#! DELETE pathToVariable` | Delete variable, useful for nested loops.| |
| Loop | **TODO: tests done: simple, stripped; tests todo: nested, with formula, special fields, through worksheets,** [ForEachCell](./src/cell/ForEachCell.ts) | 6 | #! FOR_EACH item items | Begin the loop named `item`, set the first element of `items` into `item` and go to the beginning of next line.| Connected to: `ContinueCell`, `EndLoopCell`, `DeleteCell`, `FinishedCell`, `SumCell`, `AverageCell`. |
| Loop | [ContinueCell](./src/cell/ContinueCell.ts) | 9 | `#! CONTINUE item` | Iterate to next element of loop named `item` (check `ForEachCell` for more information) and navigate to the beginning of new line. | |
| Loop | [EndLoopCell](./src/cell/EndLoopCell.ts) | 8 | `#! END_LOOP item` | Mark cell when the loop `item` finished. | |
| Aggregation| [SumCell](./src/cell/SumCell.ts) | 11 | `#! SUM item` | Write sum formulae for current column and the `item`'s rows. | [Example](./tests/integration/data/Renderer007-ForEach-Sum/) |
| Aggregation | [AverageCell](./src/cell/AverageCell.ts) | 12 | `#! AVERAGE item` | Write average formulae for current column and the `item`'s rows. | [Example](./tests/integration/data/Renderer009-ForEach-Average/) |
| View Model | **TODO: test - nested loop** [DeleteCell](./src/cell/DeleteCell.ts) | 14 | `#! DELETE pathToVariable` | Delete variable, useful for nested loops.| [Example](./tests/integration/data/Renderer009-ForEach-Average/) |


## Commands [PREVIOUS VERSION]:
Expand Down
20 changes: 10 additions & 10 deletions tests/integration/Renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import * as path from "path";
import {Workbook} from "exceljs";
import * as chai from 'chai'

function isDir(path: Dirent | string): boolean {
if (typeof path ==="object") return path.isDirectory();
function isDir(dirPath: Dirent | string): boolean {
if (typeof dirPath ==="object") { return dirPath.isDirectory(); }

try {
return fs.lstatSync(path).isDirectory();
return fs.lstatSync(dirPath).isDirectory();
} catch (e) {
return false;// lstatSync throws an error if path doesn't exist
return false;// lstatSync throws an error if dirPath doesn't exist
}
}

Expand Down Expand Up @@ -45,17 +45,17 @@ function assertCells(expected: Workbook, result: Workbook, factor: number = 10)
}
}

describe('INTEGRATION:: Test xlsx renderer ', function () {
describe('INTEGRATION:: Test xlsx renderer ', () => {

describe('Checking if assertCells works ok.', function () {
it('Same - should pass ok', async function () {
describe('Checking if assertCells works ok.', () => {
it('Same - should pass ok', async () => {
const expected = await new Workbook().xlsx.readFile(path.join(__dirname, 'data', 'assertCells', 'main.xlsx'));
const correct = await new Workbook().xlsx.readFile(path.join(__dirname, 'data', 'assertCells', 'correct.xlsx'));

assertCells(expected, correct, 20);
});

it('Different - attempt to broke assertions', async function () {
it('Different - attempt to broke assertions', async () => {
const expected = await new Workbook().xlsx.readFile(path.join(__dirname, 'data', 'assertCells', 'main.xlsx'));
const failedWorksheetAmount = await new Workbook().xlsx.readFile(path.join(__dirname, 'data', 'assertCells', 'f-ws-amount.xlsx'));
const failedWorksheetNames = await new Workbook().xlsx.readFile(path.join(__dirname, 'data', 'assertCells', 'f-ws-names.xlsx'));
Expand All @@ -76,7 +76,7 @@ describe('INTEGRATION:: Test xlsx renderer ', function () {
chai.expect(() => assertCells(expected, failedTable, 20)).throw('expected { Object (font, border, ...) } to deeply equal { Object (font, border, ...) }');
});
});
describe('Load examples, render and compare with expected result', function () {
describe('Load examples, render and compare with expected result', () => {
const dataPath = path.normalize(path.join(__dirname, 'data/'));
const sets = fs.readdirSync(path.normalize(dataPath), {withFileTypes: true})
.filter(isDir)
Expand All @@ -85,7 +85,7 @@ describe('INTEGRATION:: Test xlsx renderer ', function () {

const renderer = new Renderer();
sets.forEach(s => {
it(`Test for ${s.name}`, async function () {
it(`Test for ${s.name}`, async () => {
const result = await renderer.renderFromFile(
path.join(dataPath, s.name, "template.xlsx"),
require(path.join(dataPath, s.name, 'viewModel.json'))
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"projects": [
{
"name": "ExcelJS",
"role": "maintainer",
"platform": "github",
"link": "https://github.com/exceljs/exceljs",
"stars": 5300,
"forks": 682
},
{
"name": "xlsx-import",
"role": "owner",
"platform": "github",
"link": "https://github.com/siemienik/xlsx-import",
"stars": 2,
"forks": 0
},
{
"name": "xlsx-import",
"role": "owner",
"platform": "npm",
"link": "https://www.npmjs.com/package/xlsx-import",
"stars": "n.o.",
"forks": "n.o."
},
{
"name": "xlsx-renderer",
"role": "owner",
"platform": "github",
"link": "https://github.com/siemienik/xlsx-renderer",
"stars": 2,
"forks": 0
},
{
"name": "xlsx-renderer",
"role": "owner",
"platform": "npm",
"link": "https://www.npmjs.com/package/xlsx-renderer",
"stars": "n.o.",
"forks": "n.o."
},
{
"name": "TS Package Structure",
"role": "owner",
"platform": "github",
"link": "https://github.com/Siemienik/ts-package-structure",
"stars": 3,
"forks": 0
}
]
}
Binary file not shown.
Binary file not shown.
52 changes: 52 additions & 0 deletions tests/integration/data/Renderer007-ForEach-Sum/viewModel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"projects": [
{
"name": "ExcelJS",
"role": "maintainer",
"platform": "github",
"link": "https://github.com/exceljs/exceljs",
"stars": 5300,
"forks": 682
},
{
"name": "xlsx-import",
"role": "owner",
"platform": "github",
"link": "https://github.com/siemienik/xlsx-import",
"stars": 2,
"forks": 0
},
{
"name": "xlsx-import",
"role": "owner",
"platform": "npm",
"link": "https://www.npmjs.com/package/xlsx-import",
"stars": "n.o.",
"forks": "n.o."
},
{
"name": "xlsx-renderer",
"role": "owner",
"platform": "github",
"link": "https://github.com/siemienik/xlsx-renderer",
"stars": 2,
"forks": 0
},
{
"name": "xlsx-renderer",
"role": "owner",
"platform": "npm",
"link": "https://www.npmjs.com/package/xlsx-renderer",
"stars": "n.o.",
"forks": "n.o."
},
{
"name": "TS Package Structure",
"role": "owner",
"platform": "github",
"link": "https://github.com/Siemienik/ts-package-structure",
"stars": 3,
"forks": 0
}
]
}
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/integration/data/Renderer008-Delete/viewModel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"myName":"Paweł Siemienik"
}
Binary file not shown.
Binary file not shown.
52 changes: 52 additions & 0 deletions tests/integration/data/Renderer009-ForEach-Average/viewModel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"projects": [
{
"name": "ExcelJS",
"role": "maintainer",
"platform": "github",
"link": "https://github.com/exceljs/exceljs",
"stars": 5300,
"forks": 682
},
{
"name": "xlsx-import",
"role": "owner",
"platform": "github",
"link": "https://github.com/siemienik/xlsx-import",
"stars": 2,
"forks": 0
},
{
"name": "xlsx-import",
"role": "owner",
"platform": "npm",
"link": "https://www.npmjs.com/package/xlsx-import",
"stars": "n.o.",
"forks": "n.o."
},
{
"name": "xlsx-renderer",
"role": "owner",
"platform": "github",
"link": "https://github.com/siemienik/xlsx-renderer",
"stars": 2,
"forks": 0
},
{
"name": "xlsx-renderer",
"role": "owner",
"platform": "npm",
"link": "https://www.npmjs.com/package/xlsx-renderer",
"stars": "n.o.",
"forks": "n.o."
},
{
"name": "TS Package Structure",
"role": "owner",
"platform": "github",
"link": "https://github.com/Siemienik/ts-package-structure",
"stars": 3,
"forks": 0
}
]
}

0 comments on commit 9bd579a

Please sign in to comment.