Jasmine exercise

Learning Jasmine

by eitanp461

HTML

<script src="https://jasmine.github.io/2.4/lib/jasmine.js"></script>
<script src="https://jasmine.github.io/2.4/lib/jasmine-html.js"></script>
<script src="https://jasmine.github.io/2.4/lib/boot.js"></script>
<link rel="stylesheet" href="https://jasmine.github.io/2.4/lib/jasmine.css">

JavaScript

/*****************************************************************/
/*                       Source code                             */
/*****************************************************************/
"use strict";

// Get results faster for failing async specs
jasmine.DEFAULT_TIMEOUT_INTERVAL = 500;

// This is the class under test
class GreetingService {

  constructor(restService) {
    this.restService = restService;
  }

  // Simulate a synchronous client service
  getHelloMessage() {
    return 'Hello World!';
  }


  // Simulate an async service calling the server.
  // Callback parameter should accept the String message to display
  getGoodbyeMessage(callback) {
    // Simulate async call to server
    const entity = this.restService.getEntity();
    // Callback is called after X milliseconds to simulate response latency
    setTimeout(() => {
      callback('Goodbye ' + entity);
    }, 100);
  };

  // Spice things up by returning a promise
  getSpanishMessage(name) {
    const promise = new Promise((resolve, reject) => {
      if (name !== 'aaa') {
        resolve('Hola ' + name);
      } else {
        reject(new Error("aaa is banned"));
      }
    });
    return promise;
  }

  // Add a method that throws
  getFrenchMessage(name) {
    throw new Error('Yikes! not implemented yet')
  }
};

/*****************************************************************/
/*            Jasmine Unit test code                             */
/*****************************************************************/
describe('Greeting Service', () => {
  let service;

  // Runs before each test
  beforeEach(() => {
    this.restServiceMock = jasmine.createSpyObj('RestService', ['getEntity']);
    service = new GreetingService(this.restServiceMock);
  });

  it('returns \"Hello World!\"', () => {
    fail(Error('Check the return value of getHelloMessage!'));
  });

  it('Checks that getHelloMessage does not return \"Goodbye World!\"', () => {
    fail(Error('How do you...