Skip to content

Pallavi-5-testing-components #21

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 1 addition & 0 deletions 5-testing-components/examples/1-link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @returns {HTMLAnchorElement} a rendered anchor element
*/
export const link = (text, url, target = '_blank') => {
// <a href="https://www.google.com/">TEST Google</a>
const aEl = document.createElement('a');
aEl.href = url;
aEl.target = target;
Expand Down
9 changes: 5 additions & 4 deletions 5-testing-components/examples/1-link/link.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
</head>

<body>
<a href="https://www.google.com/" target="_blank">TEST Google</a>
<script type="module">
import { showComponentTestCase } from '/lib/show-component-test-case.js';

import { link } from './link.js';

showComponentTestCase(
'open google in a new tab (default target)',
link,
'google it',
'open google in a new tab (default target)', // text
link, // component also called function
'google it', // arguments test for the components
'https://www.google.com/',
);

showComponentTestCase(
'open a pre-defined search in a new tab',
link,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,53 @@ describe('classyHeader: renders DOM headers of different levels and classes', ()
describe('an H3 with text: "hello", and no classes', () => {
const actual = classyHeader(3, 'hello');
it('has tagName "H3"', () => {
_;
expect(actual.tagName).toEqual('H3');
});
it('has innerHTML: "hello"', () => {
_;
expect(actual.innerHTML).toEqual('hello');
});
it('has classList length 0', () => {
_;
expect(actual.classList.length).toEqual(0);
});
it('has className: ""', () => {
_;
expect(actual.className).toEqual('');
});
it('has childElementCount: 0', () => {
_;
expect(actual.childElementCount).toEqual(0);
});
});

describe('an H1 with text: "good bye", and two classes', () => {
const actual = classyHeader(_, _, _);
const actual = classyHeader(1, 'good bye', ['fancy', 'hover-right']);
it('has tagName "H1"', () => {
_;
expect(actual.tagName).toEqual('H1');
});
it('has innerHTML: "good bye"', () => {
_;
expect(actual.innerHTML).toEqual('good bye');
});
it('has classList length 2', () => {
_;
expect(actual.classList.length).toEqual(2);
});
it('have class: "fancy"', () => {
_;
it('has class: "fancy"', () => {
expect(actual.classList.contains('fancy')).toEqual(true);
});
it('have class: "hover-right"', () => {
_;
it('has class: "hover-right"', () => {
expect(actual.classList.contains('hover-right')).toEqual(true);
});
it('has childElementCount: 0', () => {
_;
expect(actual.childElementCount).toEqual(0);
});
});

describe('does not allow invalid header levels', () => {
it('throws an error if level is less than 1', () => {
const shouldThrow = () => classyHeader(0, 'oops!');
expect(shouldThrow).toThrowError(RangeError);
expect(shouldThrow).toThrow(RangeError);
});
it('throws an error if level is greater than 6', () => {
const shouldThrow = () => classyHeader(7, 'oops!');
expect(shouldThrow).toThrowError(RangeError);
expect(shouldThrow).toThrow(RangeError);
});
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@

import { classyHeader } from './classy-header.js';

showComponentTestCase('an H3 with text: "hello", and no classes');
showComponentTestCase('an H3 with text: "hello", and no classes', classyHeader, 3, 'hello');

showComponentTestCase('an H1 with text: "good bye", and two classes');
showComponentTestCase('an H1 with text: "good bye", and two classes', classyHeader, 1, 'good bye' ,['class1', 'class2']);

showComponentTestCase('does not allow headers greater than 6');

showComponentTestCase('does not allow headers less than 1');
showComponentTestCase('does not allow headers greater than 6', () => {
// This test case should throw an error, so no need to pass arguments
classyHeader(7, 'test');
});

showComponentTestCase('does not allow headers less than 1', () => {
// This test case should throw an error, so no need to pass arguments
classyHeader(0, 'test');
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
/**
/**
* @jest-environment jsdom
*/

import { linkedHeader } from './linked-header.js';
import { linkedHeader } from './linked-header.js';

describe('linkedHeader: renders an anchor inside a heading', () => {
describe('a header linked to google', () => {
const actual = linkedHeader(_, _);

it('has tagName: "H2"', () => {
expect(actual.tagName).toEqual('H2');
});
it('has childElementCount: 1', () => {
expect(actual.childElementCount).toEqual(1);
});

describe(`first child`, () => {
const actualChild = _._[_];
it('has tagName: "A"', () => {
expect(actualChild.tagName).toEqual('A');
});
it('has href: "https://www.google.com/"', () => {
expect(actualChild.href).toEqual('https://www.google.com/');
});
it('has target: "_blank" (default)', () => {
expect(actualChild.target).toEqual('_blank');
});
it('has innerHTML: "google it"', () => {
expect(actualChild.innerHTML).toEqual('google it');
});
it('has childElementCount: 0', () => {
expect(actualChild.childElementCount).toEqual(0);
});
});
});

describe('a header linked to badgers', () => {
const actual = linkedHeader(_, _, _);

it('has tagName: "H2"', () => {
_;
});
it('has childElementCount: 1', () => {
_;
});

describe(`first child`, () => {
const actualChild = actual.children[0];
it('has tagName: "A"', () => {
_;
});
it('has href: "hhttps://badgerbadgerbadger.com/"', () => {
_;
});
it('has target: "_self"', () => {
_;
});
it('has innerHTML: "badger x 3"', () => {
_;
});
it('has childElementCount: 0', () => {
_;
});
});
});
});
describe('linkedHeader: renders an anchor inside a heading', () => {
describe('a header linked to google', () => {
const actual = linkedHeader('google it', 'https://www.google.com/');

it('has tagName: "H2"', () => {
expect(actual.tagName).toEqual('H2');
});
it('has childElementCount: 1', () => {
expect(actual.childElementCount).toEqual(1);
});

describe(`first child`, () => {
const actualChild = actual.children[0];
it('has tagName: "A"', () => {
expect(actualChild.tagName).toEqual('A');
});
it('has href: "https://www.google.com/"', () => {
expect(actualChild.href).toEqual('https://www.google.com/');
});
it('has target: "_blank" (default)', () => {
expect(actualChild.target).toEqual('_blank');
});
it('has innerHTML: "google it"', () => {
expect(actualChild.innerHTML).toEqual('google it');
});
it('has childElementCount: 0', () => {
expect(actualChild.childElementCount).toEqual(0);
});
});
});

describe('a header linked to badgers', () => {
const actual = linkedHeader('badger x 3', 'https://badgerbadgerbadger.com/', '_self');

it('has tagName: "H2"', () => {
expect(actual.tagName).toEqual('H2');
});
it('has childElementCount: 1', () => {
expect(actual.childElementCount).toEqual(1);
});

describe(`first child`, () => {
const actualChild = actual.children[0];
it('has tagName: "A"', () => {
expect(actualChild.tagName).toEqual('A');
});
it('has href: "https://badgerbadgerbadger.com/"', () => {
expect(actualChild.href).toEqual('https://badgerbadgerbadger.com/');
});
it('has target: "_self"', () => {
expect(actualChild.target).toEqual('_self');
});
it('has innerHTML: "badger x 3"', () => {
expect(actualChild.innerHTML).toEqual('badger x 3');
});
it('has childElementCount: 0', () => {
expect(actualChild.childElementCount).toEqual(0);
});
});
});
});