Skip to content

Commit

Permalink
✅ Add more test case challenges to be resolved (#6)
Browse files Browse the repository at this point in the history
* ✅ Add tests (jest)

* ✅ Add a compound test case

* Update ignore patterns of Eslint & Prettier

* Rename answer key files from '.expect.js' to '.answerkey.js'

* Rename answer key files from '.expect.js' to '.answerkey.js'

* Add Node 10.x to CI test
  • Loading branch information
dongskyler authored Jul 22, 2020
1 parent d448e9c commit 5115d4d
Show file tree
Hide file tree
Showing 26 changed files with 300 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x]
node-version: [10.x, 12.x, 14.x]

steps:
- uses: actions/checkout@v2
Expand Down
33 changes: 33 additions & 0 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Main test script
*/

const { vanilla } = require('../lib/vanilla');
const loadFileToBuffer = require('../lib/loadFileToBuffer');

describe("Test command 'vanilla'", () => {
const testNames = ['documentReady'];

testNames.forEach((testName) => {
test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
});
1 change: 1 addition & 0 deletions __tests__/testCases/appendElement.answerkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
document.body.appendChild(document.createElement('p'));
1 change: 1 addition & 0 deletions __tests__/testCases/appendElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('body').append($('<p/>'));
1 change: 1 addition & 0 deletions __tests__/testCases/cloneElement.answerkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var clonedElement = document.getElementById('about').cloneNode(true);
1 change: 1 addition & 0 deletions __tests__/testCases/cloneElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var clonedElement = $('#about').clone();
28 changes: 28 additions & 0 deletions __tests__/testCases/compoundTest-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$(document).ready(function () {
$('body').bootstrapMaterialDesign();

(() => {
$('#copy-to-username').click(
/** @this HTMLElement */
function () {
$('#target-username').val(() =>
$(this).prop('checked') ? $('#target-owner').val() : ''
);
}
);

$('#target-owner').keyup(() => {
$('#copy-to-username').prop('checked') &&
$('#target-username').val($('#target-owner').val());
});

$('#target-username').keyup(
/** @this HTMLElement */
function () {
$('#copy-to-username').prop('checked', () => {
return $(this).val() === $('#target-owner').val();
});
}
);
})();
});
1 change: 1 addition & 0 deletions __tests__/testCases/createElement.answerkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var newDiv = document.createElement('div')
1 change: 1 addition & 0 deletions __tests__/testCases/createElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var divs = $('div');
1 change: 1 addition & 0 deletions __tests__/testCases/getElementById.answerkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var footer = document.getElementById('footer');
1 change: 1 addition & 0 deletions __tests__/testCases/getElementById.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var footer = $('#footer');
1 change: 1 addition & 0 deletions __tests__/testCases/parent.answerkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var parent = document.getElementById('something').parentNode;
1 change: 1 addition & 0 deletions __tests__/testCases/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var parent = $('#something').parent();
3 changes: 3 additions & 0 deletions __tests__/testCases/selectAll.answerkey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
document.querySelectorAll(".box");

var divs = document.querySelectorAll('div');
3 changes: 3 additions & 0 deletions __tests__/testCases/selectAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(".box");

var divs = $('div');
31 changes: 31 additions & 0 deletions __tests__/testChallenges/challengeAppendElement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Test function vanilla on case 'appendElement'
*/

const { vanilla } = require('../../lib/vanilla');
const loadFileToBuffer = require('../../lib/loadFileToBuffer');

describe("Test command 'vanilla' on test challenges", () => {
const testName = 'appendElement';

test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
31 changes: 31 additions & 0 deletions __tests__/testChallenges/challengeCloneElement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Test function vanilla on case 'cloneElement'
*/

const { vanilla } = require('../../lib/vanilla');
const loadFileToBuffer = require('../../lib/loadFileToBuffer');

describe("Test command 'vanilla' on test challenges", () => {
const testName = 'cloneElement';

test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
31 changes: 31 additions & 0 deletions __tests__/testChallenges/challengeCreateElement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Test function vanilla on case 'createElement'
*/

const { vanilla } = require('../../lib/vanilla');
const loadFileToBuffer = require('../../lib/loadFileToBuffer');

describe("Test command 'vanilla' on test challenges", () => {
const testName = 'createElement';

test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
31 changes: 31 additions & 0 deletions __tests__/testChallenges/challengeGetElementById.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Test function vanilla on case 'getElementById'
*/

const { vanilla } = require('../../lib/vanilla');
const loadFileToBuffer = require('../../lib/loadFileToBuffer');

describe("Test command 'vanilla' on test challenges", () => {
const testName = 'getElementById';

test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
31 changes: 31 additions & 0 deletions __tests__/testChallenges/challengeParent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Test function vanilla on case 'parent'
*/

const { vanilla } = require('../../lib/vanilla');
const loadFileToBuffer = require('../../lib/loadFileToBuffer');

describe("Test command 'vanilla' on test challenges", () => {
const testName = 'parent';

test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
31 changes: 31 additions & 0 deletions __tests__/testChallenges/challengeSelectAll.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Test function vanilla on case 'selectAll'
*/

const { vanilla } = require('../../lib/vanilla');
const loadFileToBuffer = require('../../lib/loadFileToBuffer');

describe("Test command 'vanilla' on test challenges", () => {
const testName = 'selectAll';

test(`Vanillaize '${testName}'`, async () => {
// Input
const argv = {
_: ['vanilla', `./__tests__/testCases/${testName}.js`],
C: true,
'no-cache': true,
noCache: true,
$0: 'vaniquery',
};

// Output
const output = await vanilla(argv);

// Answer key
const answerKeyFile = `./__tests__/testCases/${testName}.answerkey.js`;
const answerKey = await loadFileToBuffer(answerKeyFile);

// Expect
expect(output).toEqual(answerKey);
});
});
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
verbose: true,
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
testMatch: ['**/__tests__/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
coveragePathIgnorePatterns: ['<rootDir>/__tests__/testChallenges/.*$'],
testPathIgnorePatterns: ['(testCases).*$'],
};
15 changes: 15 additions & 0 deletions lib/loadFileToBuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Loading a file to stream buffer
* @param {string} answerKeyFile
*/

const fs = require('fs');

module.exports = async (answerKeyFile) => {
return new Promise((resolve) => {
const stream = fs.createReadStream(answerKeyFile);
stream.on('data', (buffer) => {
resolve(buffer.toString());
});
});
};
1 change: 1 addition & 0 deletions lib/revert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Revert changes of vanillaizing a file, if its cached file is present
* @param {Object} argv
*/

const fs = require('fs');
Expand Down
17 changes: 17 additions & 0 deletions lib/vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
const fs = require('fs');
const equivalentsLib = require('./equivalentsLib.js');

/**
* Get filename from argument values
* @param {Object} argv
*/
const parseFilename = (argv) => argv._[1];

/**
* Create cache file
* @param {Object} argv
*/
const saveCacheFile = (argv) => {
try {
const file = parseFilename(argv);
Expand All @@ -21,6 +29,10 @@ const saveCacheFile = (argv) => {
}
};

/**
* Vanillaize a file
* @param {Object} argv
*/
exports.vanilla = (argv) => {
try {
const file = parseFilename(argv);
Expand Down Expand Up @@ -57,6 +69,11 @@ exports.vanilla = (argv) => {
}
};

/**
* Write vanillaized result to file
* @param {Object} argv
* @param {string} result
*/
exports.vanillaOut = (argv, result) => {
try {
const file = parseFilename(argv);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ci": "yarn --frozen-lockfile",
"format": "prettier './**/*.{js,json,md}' --write",
"lint": "eslint './**/*.js' --cache --fix",
"test": "jest",
"test": "jest ./__tests__/main.test.js",
"refresh": "rm -rf ./node_modules/ && yarn ci"
},
"husky": {
Expand Down

0 comments on commit 5115d4d

Please sign in to comment.