Skip to content
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
26 changes: 2 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
# Test Framework

An empty project with Tape to demonstrate proper unit test writing. Part of a recent Intro to
JavaScript class I taught.

## Running

Follow the usual procedure for setting up NPM:

```sh
$ npm install
````

And then run the unit tests via NPM scripts:

```sh
$ npm test
# or npm run test
```

## Files

Any file ending in `spec.js` anywhere within the source folder will be included in the running
tests. For an example, check out `src/spec.js`, which tests `src/index.js`.
# Hello

Questions and thoughs are in the comments.
42 changes: 22 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
export function double ( x ) {
return x * 2;
}

export function doubleXTimes ( x, num ) {
let result = x;

for ( let i = 1; i <= num; i++ ) {
// result = result * 2;
result = double( result );
}

return result;
const Animal = function() {};
Animal.prototype = function() {};
Animal.prototype.speak = function() {
return 'generic sound';
}

export function doubleEach ( arr ) {
let result = arr.map( double );
const Reptile = function() {};
Reptile.prototype = Object.create(Animal.prototype);
Reptile.SOUND = 'Slither slither';
Reptile.prototype.speak = function(sound = Reptile.SOUND) {
return Reptile.SOUND;
};

// const result = arr.map( x => double( x ) );
// const result = arr.map( function ( x ) {
// return double( x );
// });
const Primate = function() {};
Primate.prototype = Object.create(Animal.prototype);
Primate.SOUND = 'Ug ug';
Primate.prototype.speak = function(sound = Primate.SOUND) {
return Primate.SOUND;
};

return result;
}
const Human = function() {};
Human.prototype = Object.create(Primate.prototype);
Human.prototype.speak = function(sound) {
return sound;
};

export { Animal, Reptile, Primate, Human };
72 changes: 59 additions & 13 deletions src/spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,76 @@
import test from 'tape';
import { double, doubleXTimes, doubleEach } from './index';
import { Animal, Reptile, Primate, Human } from './index';

test( 'double fn', function ( test ) {
const actual = double( 5 );
const expected = 10;

test.equal( actual, expected, 'should double the value' );

test('Create a constructor function and a prototype for an Animal', function(test) {

const animal = new Animal();

test.ok( animal instanceof Animal, 'should create an instance of Animal' );

test.end();
});

test( 'doubleXTimes', function ( test ) {
const actual = doubleXTimes( 5, 3 );
const expected = 40;

test.equal( actual, expected, 'should double 5 three times' );

test('Create a function on Animal ‘s prototype called speak that returns the animal’s sound.', function(test) {

const animal = new Animal();
const expected = 'generic sound';
const actual = animal.speak();

test.equal( actual, expected, 'should make a generic sound when it speaks' );

test.end();
});

test( 'doubleEach', function ( test ) {
const actual = doubleEach([ 0, 1, 2 ]);
const expected = [ 0, 2, 4 ];

test.deepEqual( actual, expected, 'should double each in the array' );

test('Create two new constructors with prototypes that inherit from Animal: Reptile and Primate. Overwrite the speak method for each.', function(test) {

let actual, expected;

const reptile = new Reptile();

test.ok( reptile instanceof Reptile, 'should be an instance of Reptile' );
test.ok( reptile instanceof Animal, 'should be an instance of Animal' );

expected = Reptile.SOUND;

actual = reptile.speak();
console.log(`* Reptile says, "${actual}"`);
test.equal( actual, expected, 'should make a reptile sound when it speaks' );

const primate = new Primate();

test.ok( primate instanceof Primate, 'should be an instance of Primate' );
test.ok( primate instanceof Animal, 'should be an instance of Animal' );

expected = Primate.SOUND;
actual = primate.speak();
console.log(`* Primate, says, "${actual}"`);
test.equal( actual, expected, 'should make a primate sound when it speaks' );
test.end();
});



test('Create one more that inherits from Primate called Human.', function(test) {

let actual, expected;

const human = new Human();

test.ok( human instanceof Human, 'should be an instance of Human' );
test.ok( human instanceof Primate, 'should be an instance of Primate' );
test.ok( human instanceof Animal, 'should be an instance of Animal' );

const message = 'Hello';
expected = message;
actual = human.speak( message );
console.log(`* Human says, "${actual}"`);
test.equal( actual, expected, 'should speak what it is asked to speak' );

test.end();
});