|
| 1 | +const Player = require('../../lib/jasmine_examples/Player'); |
| 2 | +const Song = require('../../lib/jasmine_examples/Song'); |
| 3 | + |
| 4 | +describe('Player', function () { |
| 5 | + let player; |
| 6 | + let song; |
| 7 | + |
| 8 | + beforeEach(function () { |
| 9 | + player = new Player(); |
| 10 | + song = new Song(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should be able to play a Song', function () { |
| 14 | + player.play(song); |
| 15 | + expect(player.currentlyPlayingSong).toEqual(song); |
| 16 | + |
| 17 | + // demonstrates use of custom matcher |
| 18 | + expect(player).toBePlaying(song); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('when song has been paused', function () { |
| 22 | + beforeEach(function () { |
| 23 | + player.play(song); |
| 24 | + player.pause(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should indicate that the song is currently paused', function () { |
| 28 | + expect(player.isPlaying).toBeFalsy(); |
| 29 | + |
| 30 | + // demonstrates use of 'not' with a custom matcher |
| 31 | + expect(player).not.toBePlaying(song); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should be possible to resume', function () { |
| 35 | + player.resume(); |
| 36 | + expect(player.isPlaying).toBeTruthy(); |
| 37 | + expect(player.currentlyPlayingSong).toEqual(song); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + // demonstrates use of spies to intercept and test method calls |
| 42 | + it('tells the current song if the user has made it a favorite', function () { |
| 43 | + spyOn(song, 'persistFavoriteStatus'); |
| 44 | + |
| 45 | + player.play(song); |
| 46 | + player.makeFavorite(); |
| 47 | + |
| 48 | + expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true); |
| 49 | + }); |
| 50 | + |
| 51 | + //demonstrates use of expected exceptions |
| 52 | + describe('#resume', function () { |
| 53 | + it('should throw an exception if song is already playing', function () { |
| 54 | + player.play(song); |
| 55 | + |
| 56 | + expect(function () { |
| 57 | + player.resume(); |
| 58 | + }).toThrowError('song is already playing'); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments