Skip to content

Commit

Permalink
Updated the style of the examples
Browse files Browse the repository at this point in the history
* const/let instead of var
* classes
* pass our own eslint checks
  • Loading branch information
sgravrock committed Sep 17, 2022
1 parent 59848ca commit 44f331f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
function Player() {
}
Player.prototype.play = function(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
};

Player.prototype.pause = function() {
this.isPlaying = false;
};
class Player {
play(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
}

Player.prototype.resume = function() {
if (this.isPlaying) {
throw new Error("song is already playing");
pause() {
this.isPlaying = false;
}

this.isPlaying = true;
};
resume() {
if (this.isPlaying) {
throw new Error('song is already playing');
}

Player.prototype.makeFavorite = function() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
};
this.isPlaying = true;
}

makeFavorite() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
}
}

module.exports = Player;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ beforeEach(function () {
toBePlaying: function () {
return {
compare: function (actual, expected) {
var player = actual;
const player = actual;

return {
pass: player.currentlyPlayingSong === expected && player.isPlaying
}
};
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
describe("Player", function() {
var Player = require('../../lib/jasmine_examples/Player');
var Song = require('../../lib/jasmine_examples/Song');
var player;
var song;
const Player = require('../../lib/jasmine_examples/Player');
const Song = require('../../lib/jasmine_examples/Song');

describe('Player', function() {
let player;
let song;

beforeEach(function() {
player = new Player();
song = new Song();
});

it("should be able to play a Song", function() {
it('should be able to play a Song', function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(song);

//demonstrates use of custom matcher
// demonstrates use of custom matcher
expect(player).toBePlaying(song);
});

describe("when song has been paused", function() {
describe('when song has been paused', function() {
beforeEach(function() {
player.play(song);
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();

// demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song);
});

it("should be possible to resume", function() {
it('should be possible to resume', function() {
player.resume();
expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song);
});
});

// 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');

player.play(song);
Expand All @@ -48,13 +49,13 @@ describe("Player", function() {
});

//demonstrates use of expected exceptions
describe("#resume", function() {
it("should throw an exception if song is already playing", function() {
describe('#resume', function() {
it('should throw an exception if song is already playing', function() {
player.play(song);

expect(function() {
player.resume();
}).toThrowError("song is already playing");
}).toThrowError('song is already playing');
});
});
});
24 changes: 12 additions & 12 deletions lib/jasmine-core/example/spec/PlayerSpec.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
describe("Player", function() {
var player;
var song;
describe('Player', function() {
let player;
let song;

beforeEach(function() {
player = new Player();
song = new Song();
});

it("should be able to play a Song", function() {
it('should be able to play a Song', function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(song);

//demonstrates use of custom matcher
// demonstrates use of custom matcher
expect(player).toBePlaying(song);
});

describe("when song has been paused", function() {
describe('when song has been paused', function() {
beforeEach(function() {
player.play(song);
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();

// demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song);
});

it("should be possible to resume", function() {
it('should be possible to resume', function() {
player.resume();
expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song);
});
});

// 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');

player.play(song);
Expand All @@ -46,13 +46,13 @@ describe("Player", function() {
});

//demonstrates use of expected exceptions
describe("#resume", function() {
it("should throw an exception if song is already playing", function() {
describe('#resume', function() {
it('should throw an exception if song is already playing', function() {
player.play(song);

expect(function() {
player.resume();
}).toThrowError("song is already playing");
}).toThrowError('song is already playing');
});
});
});
2 changes: 1 addition & 1 deletion lib/jasmine-core/example/spec/SpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ beforeEach(function () {
toBePlaying: function () {
return {
compare: function (actual, expected) {
var player = actual;
const player = actual;

return {
pass: player.currentlyPlayingSong === expected && player.isPlaying
Expand Down
36 changes: 18 additions & 18 deletions lib/jasmine-core/example/src/Player.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
function Player() {
}
Player.prototype.play = function(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
};

Player.prototype.pause = function() {
this.isPlaying = false;
};
class Player {
play(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
}

Player.prototype.resume = function() {
if (this.isPlaying) {
throw new Error("song is already playing");
pause() {
this.isPlaying = false;
}

this.isPlaying = true;
};
resume() {
if (this.isPlaying) {
throw new Error('song is already playing');
}

Player.prototype.makeFavorite = function() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
};
this.isPlaying = true;
}

makeFavorite() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
}
}
11 changes: 5 additions & 6 deletions lib/jasmine-core/example/src/Song.js
Original file line number Diff line number Diff line change
@@ -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");
};

0 comments on commit 44f331f

Please sign in to comment.