Updated the style of the examples

* const/let instead of var
* classes
* pass our own eslint checks
This commit is contained in:
Steve Gravrock
2022-09-17 12:00:20 -07:00
parent 59848ca151
commit 44f331f43d
8 changed files with 78 additions and 79 deletions

View File

@@ -1,24 +1,24 @@
function Player() { class Player {
} play(song) {
Player.prototype.play = function(song) { this.currentlyPlayingSong = song;
this.currentlyPlayingSong = song; this.isPlaying = true;
this.isPlaying = true;
};
Player.prototype.pause = function() {
this.isPlaying = false;
};
Player.prototype.resume = function() {
if (this.isPlaying) {
throw new Error("song is already playing");
} }
this.isPlaying = true; pause() {
}; this.isPlaying = false;
}
Player.prototype.makeFavorite = function() { resume() {
this.currentlyPlayingSong.persistFavoriteStatus(true); if (this.isPlaying) {
}; throw new Error('song is already playing');
}
this.isPlaying = true;
}
makeFavorite() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
}
}
module.exports = Player; module.exports = Player;

View File

@@ -1,9 +1,8 @@
function Song() { class Song {
persistFavoriteStatus(value) {
// something complicated
throw new Error('not yet implemented');
}
} }
Song.prototype.persistFavoriteStatus = function(value) {
// something complicated
throw new Error("not yet implemented");
};
module.exports = Song; module.exports = Song;

View File

@@ -3,11 +3,11 @@ beforeEach(function () {
toBePlaying: function () { toBePlaying: function () {
return { return {
compare: function (actual, expected) { compare: function (actual, expected) {
var player = actual; const player = actual;
return { return {
pass: player.currentlyPlayingSong === expected && player.isPlaying pass: player.currentlyPlayingSong === expected && player.isPlaying
} };
} }
}; };
} }

View File

@@ -1,36 +1,37 @@
describe("Player", function() { const Player = require('../../lib/jasmine_examples/Player');
var Player = require('../../lib/jasmine_examples/Player'); const Song = require('../../lib/jasmine_examples/Song');
var Song = require('../../lib/jasmine_examples/Song');
var player; describe('Player', function() {
var song; let player;
let song;
beforeEach(function() { beforeEach(function() {
player = new Player(); player = new Player();
song = new Song(); song = new Song();
}); });
it("should be able to play a Song", function() { it('should be able to play a Song', function() {
player.play(song); player.play(song);
expect(player.currentlyPlayingSong).toEqual(song); expect(player.currentlyPlayingSong).toEqual(song);
//demonstrates use of custom matcher // demonstrates use of custom matcher
expect(player).toBePlaying(song); expect(player).toBePlaying(song);
}); });
describe("when song has been paused", function() { describe('when song has been paused', function() {
beforeEach(function() { beforeEach(function() {
player.play(song); player.play(song);
player.pause(); player.pause();
}); });
it("should indicate that the song is currently paused", function() { it('should indicate that the song is currently paused', function() {
expect(player.isPlaying).toBeFalsy(); expect(player.isPlaying).toBeFalsy();
// demonstrates use of 'not' with a custom matcher // demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song); expect(player).not.toBePlaying(song);
}); });
it("should be possible to resume", function() { it('should be possible to resume', function() {
player.resume(); player.resume();
expect(player.isPlaying).toBeTruthy(); expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song); expect(player.currentlyPlayingSong).toEqual(song);
@@ -38,7 +39,7 @@ describe("Player", function() {
}); });
// demonstrates use of spies to intercept and test method calls // demonstrates use of spies to intercept and test method calls
it("tells the current song if the user has made it a favorite", function() { it('tells the current song if the user has made it a favorite', function() {
spyOn(song, 'persistFavoriteStatus'); spyOn(song, 'persistFavoriteStatus');
player.play(song); player.play(song);
@@ -48,13 +49,13 @@ describe("Player", function() {
}); });
//demonstrates use of expected exceptions //demonstrates use of expected exceptions
describe("#resume", function() { describe('#resume', function() {
it("should throw an exception if song is already playing", function() { it('should throw an exception if song is already playing', function() {
player.play(song); player.play(song);
expect(function() { expect(function() {
player.resume(); player.resume();
}).toThrowError("song is already playing"); }).toThrowError('song is already playing');
}); });
}); });
}); });

View File

@@ -1,34 +1,34 @@
describe("Player", function() { describe('Player', function() {
var player; let player;
var song; let song;
beforeEach(function() { beforeEach(function() {
player = new Player(); player = new Player();
song = new Song(); song = new Song();
}); });
it("should be able to play a Song", function() { it('should be able to play a Song', function() {
player.play(song); player.play(song);
expect(player.currentlyPlayingSong).toEqual(song); expect(player.currentlyPlayingSong).toEqual(song);
//demonstrates use of custom matcher // demonstrates use of custom matcher
expect(player).toBePlaying(song); expect(player).toBePlaying(song);
}); });
describe("when song has been paused", function() { describe('when song has been paused', function() {
beforeEach(function() { beforeEach(function() {
player.play(song); player.play(song);
player.pause(); player.pause();
}); });
it("should indicate that the song is currently paused", function() { it('should indicate that the song is currently paused', function() {
expect(player.isPlaying).toBeFalsy(); expect(player.isPlaying).toBeFalsy();
// demonstrates use of 'not' with a custom matcher // demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song); expect(player).not.toBePlaying(song);
}); });
it("should be possible to resume", function() { it('should be possible to resume', function() {
player.resume(); player.resume();
expect(player.isPlaying).toBeTruthy(); expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song); expect(player.currentlyPlayingSong).toEqual(song);
@@ -36,7 +36,7 @@ describe("Player", function() {
}); });
// demonstrates use of spies to intercept and test method calls // demonstrates use of spies to intercept and test method calls
it("tells the current song if the user has made it a favorite", function() { it('tells the current song if the user has made it a favorite', function() {
spyOn(song, 'persistFavoriteStatus'); spyOn(song, 'persistFavoriteStatus');
player.play(song); player.play(song);
@@ -46,13 +46,13 @@ describe("Player", function() {
}); });
//demonstrates use of expected exceptions //demonstrates use of expected exceptions
describe("#resume", function() { describe('#resume', function() {
it("should throw an exception if song is already playing", function() { it('should throw an exception if song is already playing', function() {
player.play(song); player.play(song);
expect(function() { expect(function() {
player.resume(); player.resume();
}).toThrowError("song is already playing"); }).toThrowError('song is already playing');
}); });
}); });
}); });

View File

@@ -3,7 +3,7 @@ beforeEach(function () {
toBePlaying: function () { toBePlaying: function () {
return { return {
compare: function (actual, expected) { compare: function (actual, expected) {
var player = actual; const player = actual;
return { return {
pass: player.currentlyPlayingSong === expected && player.isPlaying pass: player.currentlyPlayingSong === expected && player.isPlaying

View File

@@ -1,22 +1,22 @@
function Player() { class Player {
} play(song) {
Player.prototype.play = function(song) { this.currentlyPlayingSong = song;
this.currentlyPlayingSong = song; this.isPlaying = true;
this.isPlaying = true;
};
Player.prototype.pause = function() {
this.isPlaying = false;
};
Player.prototype.resume = function() {
if (this.isPlaying) {
throw new Error("song is already playing");
} }
this.isPlaying = true; pause() {
}; this.isPlaying = false;
}
Player.prototype.makeFavorite = function() { resume() {
this.currentlyPlayingSong.persistFavoriteStatus(true); if (this.isPlaying) {
}; throw new Error('song is already playing');
}
this.isPlaying = true;
}
makeFavorite() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
}
}

View File

@@ -1,7 +1,6 @@
function Song() { class Song {
persistFavoriteStatus(value) {
// something complicated
throw new Error('not yet implemented');
}
} }
Song.prototype.persistFavoriteStatus = function(value) {
// something complicated
throw new Error("not yet implemented");
};