Skip to content

Commit

Permalink
add part 5 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hampton Paulk committed Feb 27, 2018
1 parent 06253b9 commit 2646ecc
Show file tree
Hide file tree
Showing 17 changed files with 479 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"mocha:part2": "NODE_ENV=test mocha 'test/unit/mocha/part2/*.spec.js'",
"mocha:part3": "NODE_ENV=test mocha 'test/unit/mocha/part3/*.spec.js'",
"mocha:part4": "NODE_ENV=test mocha 'test/unit/mocha/part4/*.spec.js'",
"mocha:part5": "NODE_ENV=test mocha 'test/unit/mocha/part5/*.spec.js'",
"test": "npm run mocha && npm run unit",
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs",
"build": "node build/build.js"
Expand Down
4 changes: 2 additions & 2 deletions src/components/BookList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
>{{book.title}}:{{book.author}}</book-item>
</ul>
<br>
<book-form @addBook='appendBook'></book-form>
<book-form @addbook='appendBook'></book-form>
<br><hr>
<book-suggestions @appendBook='appendBook'></book-suggestions>
<book-suggestions @appendbook='appendBook'></book-suggestions>
</div>
</template>

Expand Down
30 changes: 30 additions & 0 deletions test/unit/mocha/part5/book-form-contains-data.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const esquery = require('esquery');
const esprima = require('esprima');

describe('BookForm.vue', () => {
it('should contain a data function that returns bookTitle and bookAuthor @book-form-contains-data', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm component does not exist');
}
const document = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = document.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');

const ast = esprima.parse(script[0].childNodes[0].value, { sourceType: 'module' });
const data = esquery(ast, 'Property[key.name=data]');
assert(data.length > 0, 'data function return is not present');

let results = esquery(data[0], 'Property[key.name=bookTitle] > .value[value=""]');
assert(results.length > 0, 'bookTitle property is not defined with a blank string value');

results = esquery(data[0], 'Property[key.name=bookAuthor] > .value[value=""]');
assert(results.length > 0, 'bookAuthor property is not defined with a blank string value');
});
});
33 changes: 33 additions & 0 deletions test/unit/mocha/part5/book-form-contains-form-data.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const jsdom = require('jsdom');

const { JSDOM } = jsdom;


describe('BookForm.vue', () => {
it('should a form @book-list-will-contain-form', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm.vue file does not exist');
}

// Parse document
const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = doc.childNodes;

// Parse for HTML in template
const template = nodes.filter(node => node.nodeName === 'template');
const content = parse5.serialize(template[0].content);
const dom = new JSDOM(content, { includeNodeLocations: true, SVG_LCASE: true });
const document = dom.window.document;

// Test for for form existance
const results = document.querySelector('form');
assert(results.length > 0, 'BookForm does not contain a form');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const esquery = require('esquery');
const esprima = require('esprima');

describe('BookForm.vue', () => {
it('should contain a methods declaration @book-form-contains-methods-declaration', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm component does not exist');
}
const document = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = document.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');

const ast = esprima.parse(script[0].childNodes[0].value, { sourceType: 'module' });
const methods = esquery(ast, 'Property[key.name=methods]');
assert(methods.length > 0, 'the methods declaration is not present');
});
});
32 changes: 32 additions & 0 deletions test/unit/mocha/part5/book-form-contains-props.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const esquery = require('esquery');
const esprima = require('esprima');


describe('BookForm.vue', () => {
it('should contain props @book-form-contains-props-with-books', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm.vue file does not exist');
}

// Parse document and retrieve the script section
const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = doc.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');

// Test for correct import statement
const ast = esprima.parse(script[0].childNodes[0].value, { sourceType: 'module' });

let results = esquery(ast, 'ExportDefaultDeclaration Property[key.name="name"] Literal[value="BookForm"]');
assert(results.length > 0, './BookForm does not have a name property with the value of BookForm');

results = esquery(ast, 'ExportDefaultDeclaration Property[key.name="props"] Literal[value="books"]');
assert(results.length > 0, './BookForm does not have a props property with the value of books');
});
});
30 changes: 30 additions & 0 deletions test/unit/mocha/part5/book-form-emits-add-book.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const esquery = require('esquery');
const esprima = require('esprima');

describe('BookForm.vue', () => {
it('should contain a methods call to bookSubmit @book-form-emits-add-book', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm component does not exist');
}
const document = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = document.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');

const ast = esprima.parse(script[0].childNodes[0].value, { sourceType: 'module' });
const methods = esquery(ast, 'Property[key.name=methods]');
assert(methods.length > 0, 'the methods declaration is not present');

let results = esquery(methods[0], 'Identifier[name="bookSubmit"]');
assert(results.length > 0, 'bookSubmit method is not defined');

results = esquery(methods[0], 'CallExpression[arguments] > Literal[value="addBook"]');
assert(results.length > 0, 'addBook is not present in your this call expression');
});
});
28 changes: 28 additions & 0 deletions test/unit/mocha/part5/book-form-exists.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');


describe('BookForm.vue', () => {
it('should exist with correct framework @book-form-exists', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm.vue file does not exist');
}

// Parse document and retrieve the script section
const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = doc.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');
assert(script, 'No script tag exists in BookForm.vue');

const template = nodes.filter(node => node.nodeName === 'template');
assert(template, 'No template tag exists in BookForm.vue');

const style = nodes.filter(node => node.nodeName === 'style');
assert(style, 'No style tag exists in BookForm.vue');
});
});
37 changes: 37 additions & 0 deletions test/unit/mocha/part5/book-form-form-has-on-submit.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const jsdom = require('jsdom');

const { JSDOM } = jsdom;


describe('BookForm.vue', () => {
it('should contain a form with submit call to bookSubmit @book-list-will-contain-submit-with-prevent-and-method-call', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm.vue file does not exist');
}

// Parse document
const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = doc.childNodes;

// Parse for HTML in template
const template = nodes.filter(node => node.nodeName === 'template');
const content = parse5.serialize(template[0].content);
const dom = new JSDOM(content, { includeNodeLocations: true, SVG_LCASE: true });
const document = dom.window.document;

// Test for for form existance
const results = document.querySelector('form');
assert(results.length > 0, 'BookForm does not contain a form');

assert(results.outerHTML.includes('submit.prevent'), 'BookForm does not have an v-on:submit.prevent');

assert(results.outerHTML.includes('bookSubmit(bookTitle, bookAuthor)'), 'BookForm does not call bookSubmit');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const jsdom = require('jsdom');

const { JSDOM } = jsdom;


describe('BookForm.vue', () => {
it('should contain a form with v-model directives @book-list-will-contain-v-model-directives', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm.vue file does not exist');
}

// Parse document
const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = doc.childNodes;

// Parse for HTML in template
const template = nodes.filter(node => node.nodeName === 'template');
const content = parse5.serialize(template[0].content);
const dom = new JSDOM(content, { includeNodeLocations: true, SVG_LCASE: true });
const document = dom.window.document;

// Test for for form existance
const results = document.querySelector('form');
assert(results.length > 0, 'BookForm does not contain a form');

assert(results.innerHTML.includes('v-model="bookTitle"'), 'BookForm does not have an v-model for bookTitle');

assert(results.innerHTML.includes('v-model="bookAuthor"'), 'BookForm does not have an v-model for bookAuthor');
});
});
33 changes: 33 additions & 0 deletions test/unit/mocha/part5/book-form-methods-contains-add-book.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const esquery = require('esquery');
const esprima = require('esprima');

describe('BookForm.vue', () => {
it('should contain a methods call to bookSubmit @book-form-contains-method-call', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookForm.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookForm component does not exist');
}
const document = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = document.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');

const ast = esprima.parse(script[0].childNodes[0].value, { sourceType: 'module' });
const methods = esquery(ast, 'Property[key.name=methods]');
assert(methods.length > 0, 'the methods declaration is not present');

let results = esquery(methods[0], 'Identifier[name="bookSubmit"]');
assert(results.length > 0, 'bookSubmit method is not defined');

results = esquery(methods[0], 'Property[key.name="bookSubmit"] > FunctionExpression > Identifier[name="bookTitle"]');
assert(results.length > 0, 'bookTitle is not an argument inside the the bookSubmit Method');

results = esquery(methods[0], 'Property[key.name="bookSubmit"] > FunctionExpression > Identifier[name="bookAuthor"]');
assert(results.length > 0, 'bookAuthor is not an argument inside the the bookSubmit Method');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const esquery = require('esquery');
const esprima = require('esprima');

describe('BookList.vue', () => {
it('should contain a methods call to appendBook @book-list-contains-method-call', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookList.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookList component does not exist');
}
const document = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = document.childNodes;
const script = nodes.filter(node => node.nodeName === 'script');

const ast = esprima.parse(script[0].childNodes[0].value, { sourceType: 'module' });
const methods = esquery(ast, 'Property[key.name=methods]');
assert(methods.length > 0, 'the methods declaration is not present');

let results = esquery(methods[0], 'Identifier[name="appendBook"]');
assert(results.length > 0, 'appendBook method is not defined');

results = esquery(methods[0], 'Property[key.name="appendBook"] > FunctionExpression > Identifier[name="bookTitle"]');
assert(results.length > 0, 'bookTitle is not an argument inside the the appendBook Method');

results = esquery(methods[0], 'Property[key.name="appendBook"] > FunctionExpression > Identifier[name="bookAuthor"]');
assert(results.length > 0, 'bookAuthor is not an argument inside the the appendBook Method');
});
});
33 changes: 33 additions & 0 deletions test/unit/mocha/part5/book-list-contains-book-form-element.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const parse5 = require('parse5');
const jsdom = require('jsdom');

const { JSDOM } = jsdom;


describe('BookList.vue', () => {
it('should contain book-form element @book-list-contains-book-form', () => {
let file;
try {
file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookList.vue'), 'utf8');
} catch (e) {
assert(false, 'The BookList.vue file does not exist');
}

// Parse document
const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
const nodes = doc.childNodes;

// Parse for HTML in template
const template = nodes.filter(node => node.nodeName === 'template');
const content = parse5.serialize(template[0].content);
const dom = new JSDOM(content, { includeNodeLocations: true, SVG_LCASE: true });
const document = dom.window.document;

// Test for booklist in the app div
const results = document.querySelector('div');
assert(results.innerHTML.includes('book-form'), 'BookList does not contain any book-form tags');
});
});
Loading

0 comments on commit 2646ecc

Please sign in to comment.