From 46336530737388753ae02b16803c1dae2118a899 Mon Sep 17 00:00:00 2001 From: Visrut Date: Fri, 19 Apr 2024 19:36:21 +0530 Subject: [PATCH] Migrate OrderHistoryFactory to Angular --- app-new/src/app/app.module.ts | 3 +- .../src/app/services/OrderHistoryFactory.ts | 14 +++++++++ .../src/app/tests/OrderHistoryFactory.spec.js | 31 +++++++++++++++++++ browser/js/orders/order-history.factory.js | 12 ------- 4 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 app-new/src/app/services/OrderHistoryFactory.ts create mode 100644 app-new/src/app/tests/OrderHistoryFactory.spec.js delete mode 100644 browser/js/orders/order-history.factory.js diff --git a/app-new/src/app/app.module.ts b/app-new/src/app/app.module.ts index 91f48b8..a4f157b 100644 --- a/app-new/src/app/app.module.ts +++ b/app-new/src/app/app.module.ts @@ -1,3 +1,4 @@ +import { OrderHistoryService } from './services/OrderHistoryFactory'; import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; @@ -8,7 +9,7 @@ import { UpgradeModule } from '@angular/upgrade/static'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, AppRoutingModule, UpgradeModule], - providers: [], + providers: [OrderHistoryService, ], bootstrap: [AppComponent], }) export class AppModule {} diff --git a/app-new/src/app/services/OrderHistoryFactory.ts b/app-new/src/app/services/OrderHistoryFactory.ts new file mode 100644 index 0000000..c7eac11 --- /dev/null +++ b/app-new/src/app/services/OrderHistoryFactory.ts @@ -0,0 +1,14 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Observable } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class OrderHistoryService { + constructor(private http: HttpClient) {} + + viewOrders(id: string): Observable { + return this.http.get(`/api/order/${id}`); + } +} \ No newline at end of file diff --git a/app-new/src/app/tests/OrderHistoryFactory.spec.js b/app-new/src/app/tests/OrderHistoryFactory.spec.js new file mode 100644 index 0000000..328483d --- /dev/null +++ b/app-new/src/app/tests/OrderHistoryFactory.spec.js @@ -0,0 +1,31 @@ +describe("OrderHistoryFactory", function () { + beforeEach(module("FullstackGeneratedApp")); + beforeEach(module("$$UpgradeModule")); + + var $controller, $rootScope, $scope, $httpBackend, OrderHistoryFactory; + + beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_, _OrderHistoryFactory_) { + $controller = _$controller_; + $rootScope = _$rootScope_; + $scope = $rootScope.$new(); + $httpBackend = _$httpBackend_; + OrderHistoryFactory = _OrderHistoryFactory_; + })); + + describe("viewOrders", function () { + it("should fetch order history for a given id", function () { + var testId = 123; + var mockResponse = { data: "mock order data" }; + $httpBackend.expectGET("/api/order/" + testId).respond(mockResponse); + + var orderHistory; + OrderHistoryFactory.viewOrders(testId).then(function (data) { + orderHistory = data; + }); + + $httpBackend.flush(); + + expect(orderHistory).toEqual(mockResponse.data); + }); + }); +}); \ No newline at end of file diff --git a/browser/js/orders/order-history.factory.js b/browser/js/orders/order-history.factory.js deleted file mode 100644 index 38acbda..0000000 --- a/browser/js/orders/order-history.factory.js +++ /dev/null @@ -1,12 +0,0 @@ -app.factory("OrderHistoryFactory", function ($http) { - var OrderHistoryFactory = {}; - - OrderHistoryFactory.viewOrders = function (id) { - return $http.get("/api/order/" + id).then(function (response) { - console.log("VIEW MY ORDERS", response.data); - return response.data; - }); - }; - - return OrderHistoryFactory; -});