From 553d1d6ffd770947ce3130a21896b1bb8ce5bd7f Mon Sep 17 00:00:00 2001
From: Lotte Hofstede <lotte_hofstede@hotmail.com>
Date: Tue, 28 Nov 2017 15:00:28 +0100
Subject: [PATCH] 130: tests for uuid index reducer

---
 src/app/core/data/request.reducer.spec.ts     | 72 +------------------
 src/app/core/index/uuid-index.reducer.spec.ts | 56 +++++++++++++++
 2 files changed, 59 insertions(+), 69 deletions(-)
 create mode 100644 src/app/core/index/uuid-index.reducer.spec.ts

diff --git a/src/app/core/data/request.reducer.spec.ts b/src/app/core/data/request.reducer.spec.ts
index 4ae3757e89..d9a58b4107 100644
--- a/src/app/core/data/request.reducer.spec.ts
+++ b/src/app/core/data/request.reducer.spec.ts
@@ -35,14 +35,14 @@ describe('requestReducer', () => {
     expect(newState).toEqual(testState);
   });
 
-  it('should start with an empty cache', () => {
+  it('should start with an empty state', () => {
     const action = new NullAction();
     const initialState = requestReducer(undefined, action);
 
     expect(initialState).toEqual(Object.create(null));
   });
 
-  it('should add the new RestRequest and set \'requestPending\' to true, \'responsePending\' to false and \'completed\' to false for the given RestRequest in the state, in response to an CONFIGURE action', () => {
+  it('should add the new RestRequest and set \'requestPending\' to true, \'responsePending\' to false and \'completed\' to false for the given RestRequest in the state, in response to a CONFIGURE action', () => {
     const state = testState;
     const request = new RestRequest(link2);
 
@@ -66,7 +66,7 @@ describe('requestReducer', () => {
     expect(newState[link1].responsePending).toEqual(true);
     expect(newState[link1].completed).toEqual(state[link1].completed);
   });
-  it('should leave \'requestPending\' untouched, set \'responsePending\' to false and \'completed\' to false for the given RestRequest in the state, in response to an COMPLETE action', () => {
+  it('should leave \'requestPending\' untouched, set \'responsePending\' to false and \'completed\' to false for the given RestRequest in the state, in response to a COMPLETE action', () => {
     const state = testState;
 
     const action = new RequestCompleteAction(link1);
@@ -77,70 +77,4 @@ describe('requestReducer', () => {
     expect(newState[link1].responsePending).toEqual(false);
     expect(newState[link1].completed).toEqual(true);
   });
-
-  //
-  // it('should overwrite an object in the cache in response to an ADD action if it already exists', () => {
-  //   const objectToCache = { self: selfLink1, foo: 'baz', somethingElse: true };
-  //   const timeAdded = new Date().getTime();
-  //   const msToLive = 900000;
-  //   const requestHref = 'https://rest.api/endpoint/selfLink1';
-  //   const action = new AddToObjectCacheAction(objectToCache, timeAdded, msToLive, requestHref);
-  //   const newState = requestReducer(testState, action);
-  //
-  //   /* tslint:disable:no-string-literal */
-  //   expect(newState[selfLink1].data['foo']).toBe('baz');
-  //   expect(newState[selfLink1].data['somethingElse']).toBe(true);
-  //   /* tslint:enable:no-string-literal */
-  // });
-  //
-  // it('should perform the ADD action without affecting the previous state', () => {
-  //   const state = Object.create(null);
-  //   const objectToCache = { self: selfLink1 };
-  //   const timeAdded = new Date().getTime();
-  //   const msToLive = 900000;
-  //   const requestHref = 'https://rest.api/endpoint/selfLink1';
-  //   const action = new AddToObjectCacheAction(objectToCache, timeAdded, msToLive, requestHref);
-  //   deepFreeze(state);
-  //
-  //   requestReducer(state, action);
-  // });
-  //
-  // it('should remove the specified object from the cache in response to the REMOVE action', () => {
-  //   const action = new RemoveFromObjectCacheAction(selfLink1);
-  //   const newState = requestReducer(testState, action);
-  //
-  //   expect(testState[selfLink1]).not.toBeUndefined();
-  //   expect(newState[selfLink1]).toBeUndefined();
-  // });
-  //
-  // it("shouldn't do anything in response to the REMOVE action for an object that isn't cached", () => {
-  //   const wrongKey = "this isn't cached";
-  //   const action = new RemoveFromObjectCacheAction(wrongKey);
-  //   const newState = requestReducer(testState, action);
-  //
-  //   expect(testState[wrongKey]).toBeUndefined();
-  //   expect(newState).toEqual(testState);
-  // });
-  //
-  // it('should perform the REMOVE action without affecting the previous state', () => {
-  //   const action = new RemoveFromObjectCacheAction(selfLink1);
-  //   // testState has already been frozen above
-  //   requestReducer(testState, action);
-  // });
-  //
-  // it('should set the timestamp of all objects in the cache in response to a RESET_TIMESTAMPS action', () => {
-  //   const newTimestamp = new Date().getTime();
-  //   const action = new ResetObjectCacheTimestampsAction(newTimestamp);
-  //   const newState = requestReducer(testState, action);
-  //   Object.keys(newState).forEach((key) => {
-  //     expect(newState[key].timeAdded).toEqual(newTimestamp);
-  //   });
-  // });
-  //
-  // it('should perform the RESET_TIMESTAMPS action without affecting the previous state', () => {
-  //   const action = new ResetObjectCacheTimestampsAction(new Date().getTime());
-  //   // testState has already been frozen above
-  //   requestReducer(testState, action);
-  // });
-
 });
diff --git a/src/app/core/index/uuid-index.reducer.spec.ts b/src/app/core/index/uuid-index.reducer.spec.ts
new file mode 100644
index 0000000000..7b96096604
--- /dev/null
+++ b/src/app/core/index/uuid-index.reducer.spec.ts
@@ -0,0 +1,56 @@
+import * as deepFreeze from 'deep-freeze';
+
+import { uuidIndexReducer, UUIDIndexState } from './uuid-index.reducer';
+import { AddToUUIDIndexAction, RemoveHrefFromUUIDIndexAction } from './uuid-index.actions';
+
+class NullAction extends AddToUUIDIndexAction {
+  type = null;
+  payload = null;
+
+  constructor() {
+    super(null, null);
+  }
+}
+
+describe('requestReducer', () => {
+  const link1 = 'https://dspace7.4science.it/dspace-spring-rest/api/core/items/567a639f-f5ff-4126-807c-b7d0910808c8';
+  const link2 = 'https://dspace7.4science.it/dspace-spring-rest/api/core/items/1911e8a4-6939-490c-b58b-a5d70f8d91fb';
+  const uuid1 = '567a639f-f5ff-4126-807c-b7d0910808c8';
+  const uuid2 = '1911e8a4-6939-490c-b58b-a5d70f8d91fb';
+  const testState: UUIDIndexState = {
+    [uuid1]: link1
+  };
+  deepFreeze(testState);
+
+  it('should return the current state when no valid actions have been made', () => {
+    const action = new NullAction();
+    const newState = uuidIndexReducer(testState, action);
+
+    expect(newState).toEqual(testState);
+  });
+
+  it('should start with an empty state', () => {
+    const action = new NullAction();
+    const initialState = uuidIndexReducer(undefined, action);
+
+    expect(initialState).toEqual(Object.create(null));
+  });
+
+  it('should add the \'uuid\' with the corresponding \'href\' to the state, in response to an ADD action', () => {
+    const state = testState;
+
+    const action = new AddToUUIDIndexAction(uuid2, link2);
+    const newState = uuidIndexReducer(state, action);
+
+    expect(newState[uuid2]).toEqual(link2);
+  });
+
+  it('should remove the given \'href\' from it\'s corresponding \'uuid\' in the state, in response to a REMOVE_HREF action', () => {
+    const state = testState;
+
+    const action = new RemoveHrefFromUUIDIndexAction(link1);
+    const newState = uuidIndexReducer(state, action);
+
+    expect(newState[uuid1]).toBeUndefined();
+  });
+});
-- 
GitLab