Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vsakaria/2c28c6cac2ac2bf7a62630cb4f0e211c to your computer and use it in GitHub Desktop.

Select an option

Save vsakaria/2c28c6cac2ac2bf7a62630cb4f0e211c to your computer and use it in GitHub Desktop.

Revisions

  1. vsakaria renamed this gist Sep 26, 2016. 1 changed file with 0 additions and 0 deletions.
  2. vsakaria revised this gist Sep 26, 2016. No changes.
  3. vsakaria revised this gist Sep 26, 2016. No changes.
  4. vsakaria revised this gist Sep 26, 2016. 1 changed file with 7 additions and 43 deletions.
    50 changes: 7 additions & 43 deletions http.spec.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    import { ReflectiveInjector } from "@angular/core";
    import { async, inject, TestBed } from "@angular/core/testing";
    import { Http, HttpModule, BaseRequestOptions, RequestMethod, Response, ResponseOptions } from "@angular/http";
    import { MockBackend } from "@angular/http/testing";
    @@ -27,18 +26,20 @@ describe("QMSService", () => {
    let response: any;
    let request: any;

    let HTTP = {
    provide: Http,
    useFactory: (backend: MockBackend, options: BaseRequestOptions) => new Http(backend, options),
    deps: [MockBackend, BaseRequestOptions]
    }

    // Set up mocks for Http backend and mock session service
    beforeEach(() => {
    TestBed.configureTestingModule({
    imports: [HttpModule],
    providers: [
    MockBackend,
    BaseRequestOptions,
    {
    provide: Http,
    useFactory: (backend: MockBackend, options: BaseRequestOptions) => new Http(backend, options),
    deps: [MockBackend, BaseRequestOptions]
    },
    HTTP,
    QMSService,
    { provide: SessionService, useClass: MockSessionService }
    ]
    @@ -53,19 +54,6 @@ describe("QMSService", () => {

    describe("createTraversal", () => {

    it("should provide a POST request", async(() => {

    backend.connections.subscribe((c: any) => request = c.request);

    qmsService.createTraversal(123)
    .subscribe((jsr: any) => response = jsr);

    expect(request.method).toEqual(RequestMethod.Post);
    expect(request.url).toBe("http://dt-delivery-01/api/questionnaire/traversals");
    expect(request.text()).toEqual(JSON.stringify({"traversalTarget": {"productId": 123}}));
    expect(request.headers.get("Content-Type")).toEqual("application/json");
    }));

    it("should get a response", async(() => {

    SetupTest.mockConnection(backend);
    @@ -79,17 +67,6 @@ describe("QMSService", () => {

    describe("getCurrentQuestions", () => {

    it("should provide a GET request", async(() => {

    backend.connections.subscribe((c: any) => request = c.request);

    qmsService.getCurrentQuestions("/api/current")
    .subscribe((jsr: any) => response = jsr);

    expect(request.method).toEqual(RequestMethod.Get);
    expect(request.url).toBe("http://dt-delivery-01/api/current");
    }));

    it("should get a response", async(() => {

    SetupTest.mockConnection(backend);
    @@ -103,19 +80,6 @@ describe("QMSService", () => {

    describe("When answering a question", () => {

    it("should provide a POST request", async(() => {

    backend.connections.subscribe((c: any) => request = c.request);

    qmsService.answerQuestion("/some_url", [{"some_key": "SOME_ANSWER"}])
    .subscribe((jsr: any) => response = jsr);

    expect(request.method).toEqual(RequestMethod.Post);
    expect(request.url).toBe("http://dt-delivery-01/some_url");
    expect(request.text()).toEqual(JSON.stringify({"answers": [{"some_key": "SOME_ANSWER"}]}));
    expect(request.headers.get("Content-Type")).toEqual("application/json");
    }));

    it("should return a response", async(() => {

    SetupTest.mockConnection(backend);
  5. vsakaria created this gist Sep 7, 2016.
    129 changes: 129 additions & 0 deletions http.spec.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,129 @@
    import { ReflectiveInjector } from "@angular/core";
    import { async, inject, TestBed } from "@angular/core/testing";
    import { Http, HttpModule, BaseRequestOptions, RequestMethod, Response, ResponseOptions } from "@angular/http";
    import { MockBackend } from "@angular/http/testing";
    import { Observable } from "rxjs";

    import { MockSessionService } from "../../mocks/session.service.mock";
    import { QMSService } from "./qms.service";
    import { SessionService } from "../session/session.service";


    class SetupTest {
    static mockConnection(mockBackend: MockBackend) {
    let options = new ResponseOptions({
    body: JSON.stringify({ myData: "Something" })
    });

    mockBackend.connections.subscribe((c: any) => c.mockRespond(new Response(options)));
    }
    }

    describe("QMSService", () => {

    let qmsService: QMSService;
    let sessionService: SessionService;
    let backend: MockBackend;
    let response: any;
    let request: any;

    // Set up mocks for Http backend and mock session service
    beforeEach(() => {
    TestBed.configureTestingModule({
    imports: [HttpModule],
    providers: [
    MockBackend,
    BaseRequestOptions,
    {
    provide: Http,
    useFactory: (backend: MockBackend, options: BaseRequestOptions) => new Http(backend, options),
    deps: [MockBackend, BaseRequestOptions]
    },
    QMSService,
    { provide: SessionService, useClass: MockSessionService }
    ]
    });
    });

    beforeEach(inject([QMSService, SessionService, MockBackend], (qs: QMSService, ss: SessionService, bkend: MockBackend) => {
    qmsService = qs;
    sessionService = ss;
    backend = bkend;
    }));

    describe("createTraversal", () => {

    it("should provide a POST request", async(() => {

    backend.connections.subscribe((c: any) => request = c.request);

    qmsService.createTraversal(123)
    .subscribe((jsr: any) => response = jsr);

    expect(request.method).toEqual(RequestMethod.Post);
    expect(request.url).toBe("http://dt-delivery-01/api/questionnaire/traversals");
    expect(request.text()).toEqual(JSON.stringify({"traversalTarget": {"productId": 123}}));
    expect(request.headers.get("Content-Type")).toEqual("application/json");
    }));

    it("should get a response", async(() => {

    SetupTest.mockConnection(backend);

    qmsService.createTraversal(123)
    .subscribe((jsr: any) => response = jsr);

    expect(response).toEqual({ myData: "Something" });
    }));
    });

    describe("getCurrentQuestions", () => {

    it("should provide a GET request", async(() => {

    backend.connections.subscribe((c: any) => request = c.request);

    qmsService.getCurrentQuestions("/api/current")
    .subscribe((jsr: any) => response = jsr);

    expect(request.method).toEqual(RequestMethod.Get);
    expect(request.url).toBe("http://dt-delivery-01/api/current");
    }));

    it("should get a response", async(() => {

    SetupTest.mockConnection(backend);

    qmsService.getCurrentQuestions("/api/current")
    .subscribe((jsr: any) => response = jsr);

    expect(response).toEqual({ myData: "Something" });
    }));
    });

    describe("When answering a question", () => {

    it("should provide a POST request", async(() => {

    backend.connections.subscribe((c: any) => request = c.request);

    qmsService.answerQuestion("/some_url", [{"some_key": "SOME_ANSWER"}])
    .subscribe((jsr: any) => response = jsr);

    expect(request.method).toEqual(RequestMethod.Post);
    expect(request.url).toBe("http://dt-delivery-01/some_url");
    expect(request.text()).toEqual(JSON.stringify({"answers": [{"some_key": "SOME_ANSWER"}]}));
    expect(request.headers.get("Content-Type")).toEqual("application/json");
    }));

    it("should return a response", async(() => {

    SetupTest.mockConnection(backend);

    qmsService.answerQuestion("/some_url", [{"some_key": "SOME_ANSWER"}])
    .subscribe((jsr: any) => response = jsr);

    expect(response).toEqual({ myData: "Something" });
    }));
    });
    });