Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate lib/nunjucks-extensions to TypeScript #2321

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ packages/core/src/variables/*.js
packages/core/test/unit/**/*.js

# TODO: remove when migrated to TS
!packages/core/test/unit/lib/nunjucks-extensions/*.js
!markdown-it-icons.test.js
!Site.test.js

Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ packages/core/src/variables/*.js
packages/core/test/unit/**/*.js

# TODO: remove when migrated to TS
!packages/core/test/unit/lib/nunjucks-extensions/*.js
!markdown-it-icons.test.js
!Site.test.js

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { filter } = require('../../../../src/lib/nunjucks-extensions/nunjucks-date');
import { filter } from '../../../../src/lib/nunjucks-extensions/nunjucks-date';

test('without format and days to add', () => {
const actual = filter('2020-01-01');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
const nunjucks = require('nunjucks');
const path = require('path');
const { SetExternalExtension } = require('../../../../src/lib/nunjucks-extensions/set-external');
import nunjucks from 'nunjucks';
import path from 'path';
import { SetExternalExtension } from '../../../../src/lib/nunjucks-extensions/set-external';

test('does not render the header row when noHeader is not set', () => {
const nj = nunjucks.configure();
const nj = nunjucks.configure({});
const setExternalExtension = new SetExternalExtension(path.resolve(__dirname, '../../../../'), nj);
nj.addExtension('SetExternalExtension', setExternalExtension);
const withHeaderCsvPath = 'test/unit/lib/nunjucks-extensions/data-with-header.csv';

// eslint-disable-next-line max-len
const template = `{% ext x = "${withHeaderCsvPath}"%}{% for item in x %}<div>{{ item.name }}{{ item.score }}</div>{% endfor %}`;
const actual = nj.renderString(template);
const actual = nj.renderString(template, {});
const expected = '<div>Alice1</div><div>Bob2</div>';
expect(actual).toEqual(expected);
});

test('renders the header row when noHeader is set', () => {
const nj = nunjucks.configure();
const nj = nunjucks.configure({});
const setExternalExtension = new SetExternalExtension(path.resolve(__dirname, '../../../../'), nj);
nj.addExtension('SetExternalExtension', setExternalExtension);
const noHeaderCsvPath = 'test/unit/lib/nunjucks-extensions/data-with-no-header.csv';

// eslint-disable-next-line max-len
const template = `{% ext x = "${noHeaderCsvPath}", noHeader %}{% for item in x %}<div>{{ item[0] }}{{ item[1] }}</div>{% endfor %}`;
const actual = nj.renderString(template);
const actual = nj.renderString(template, {});
const expected = '<div>1Alice</div><div>2Bob</div><div>3Charlie</div>';
expect(actual).toEqual(expected);
});

test('can index by [rowNum][colNum] when noHeader is set', () => {
const nj = nunjucks.configure();
const nj = nunjucks.configure({});
const setExternalExtension = new SetExternalExtension(path.resolve(__dirname, '../../../../'), nj);
nj.addExtension('SetExternalExtension', setExternalExtension);
const noHeaderCsvPath = 'test/unit/lib/nunjucks-extensions/data-with-no-header-indexing.csv';

// eslint-disable-next-line max-len
const template = `{% ext x = "${noHeaderCsvPath}", noHeader %}<div>{{ x[0][0] }}{{ x[0][1] }}{{ x[0][2] }}{{ x[1][0] }}{{ x[1][1] }}{{ x[2][0] }}</div>`;
const actual = nj.renderString(template);
const actual = nj.renderString(template, {});
const expected = '<div>123456</div>';
expect(actual).toEqual(expected);
});