Skip to content

Commit

Permalink
fix more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-trajanovski committed Nov 22, 2024
1 parent 76bd6fd commit 37260d3
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ describe("ProposalDashboardComponent", () => {
{ provide: DatasetsService, useClass: MockDatasetApi },
{
provide: LogbooksService,
useValue: jasmine.createSpyObj("logbookApi", ["find", "findByName"]),
useValue: jasmine.createSpyObj("logbookApi"),
},
{
provide: ProposalsService,
useValue: jasmine.createSpyObj("proposalApi", ["find", "findByName"]),
useValue: jasmine.createSpyObj("proposalApi"),
},
{ provide: ScicatDataService, useValue: {} },
],
Expand Down
4 changes: 3 additions & 1 deletion src/app/shared/services/thumbnail.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ describe("ThumbnailService", () => {
});

beforeEach(() => {
const datasetApiSpy = jasmine.createSpyObj("DatasetApi", ["thumbnail"]);
const datasetApiSpy = jasmine.createSpyObj("DatasetApi", [
"datasetsControllerThumbnail",
]);

TestBed.configureTestingModule({
providers: [
Expand Down
2 changes: 2 additions & 0 deletions src/app/state-management/effects/logbooks.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { selectFilters } from "state-management/selectors/logbooks.selectors";
import { Type } from "@angular/core";
import { TestObservable } from "jasmine-marbles/src/test-observables";
import { Logbook } from "shared/MockStubs";
import { HttpClientTestingModule } from "@angular/common/http/testing";

const logbook = new Logbook({
name: "test",
Expand All @@ -27,6 +28,7 @@ describe("LogbookEffects", () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
LogbookEffects,
provideMockActions(() => actions),
Expand Down
52 changes: 26 additions & 26 deletions src/app/state-management/effects/proposals.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { throwError } from "rxjs";
import { of, throwError } from "rxjs";
import { ProposalEffects } from "./proposals.effects";
import { TestBed } from "@angular/core/testing";
import { provideMockStore } from "@ngrx/store/testing";
Expand All @@ -10,17 +10,17 @@ import {
} from "state-management/selectors/proposals.selectors";
import * as fromActions from "state-management/actions/proposals.actions";
import { hot, cold } from "jasmine-marbles";
import {
loadingAction,
loadingCompleteAction,
} from "state-management/actions/user.actions";
import { Type } from "@angular/core";
import {
DatasetsService,
ProposalsService,
} from "@scicatproject/scicat-sdk-ts";
import { TestObservable } from "jasmine-marbles/src/test-observables";
import { Attachment, Dataset, Proposal } from "shared/MockStubs";
import {
loadingAction,
loadingCompleteAction,
} from "state-management/actions/user.actions";

const proposal = new Proposal({
proposalId: "testId",
Expand Down Expand Up @@ -68,6 +68,7 @@ describe("ProposalEffects", () => {
provide: ProposalsService,
useValue: jasmine.createSpyObj("proposalApi", [
"proposalsControllerFullquery",
"proposalsControllerFullfacet",
"proposalsControllerFindById",
"proposalsControllerFindByIdAccess",
"proposalsControllerCreateAttachment",
Expand Down Expand Up @@ -228,21 +229,26 @@ describe("ProposalEffects", () => {
count: proposals.length,
});

const responseArray = [
{
all: [{ totalSets: proposals.length }],
},
];
actions = hot("-a", { a: action });
const response = cold("-a|", { a: proposals });
proposalApi.proposalsControllerFullquery.and.returnValue(response);
const response = cold("-a|", { a: responseArray });
proposalApi.proposalsControllerFullfacet.and.returnValue(response);

const expected = cold("--b", { b: outcome });
expect(effects.fetchCount$).toBeObservable(expected);
});

it("should result in a fetchProposalsFailedAction", () => {
it("should result in a fetchCountFailedAction", () => {
const action = fromActions.fetchCountAction();
const outcome = fromActions.fetchCountFailedAction();

actions = hot("-a", { a: action });
const response = cold("-#", {});
proposalApi.proposalsControllerFullquery.and.returnValue(response);
proposalApi.proposalsControllerFullfacet.and.returnValue(response);

const expected = cold("--b", { b: outcome });
expect(effects.fetchCount$).toBeObservable(expected);
Expand All @@ -252,23 +258,21 @@ describe("ProposalEffects", () => {
describe("fetchProposal$", () => {
const proposalId = "testId";
const permission = {
accepted: { canAccess: true },
rejected: { canAccess: false },
accepted: { canAccess: true } as any,
rejected: { canAccess: false } as any,
};

// TODO: For now the tests are passing but check the types and fix any types
it("should result in a fetchProposalCompleteAction", () => {
const action = fromActions.fetchProposalAction({ proposalId });
const outcome = fromActions.fetchProposalCompleteAction({ proposal });

const responseAccess = cold("-#", permission.accepted);
const responseProposal = cold("-#", proposal);

proposalApi.proposalsControllerFindByIdAccess
.withArgs(proposalId)
.and.returnValue(responseAccess);
.and.returnValue(of(permission.accepted));
proposalApi.proposalsControllerFindById
.withArgs(encodeURIComponent(proposalId))
.and.returnValue(responseProposal);
.and.returnValue(of(proposal as any));

actions = hot("a", { a: action });
const expected = cold("b", { b: outcome });
Expand All @@ -278,31 +282,27 @@ describe("ProposalEffects", () => {

it("should result in a fetchProposalFailedAction", () => {
const action = fromActions.fetchProposalAction({ proposalId });
const failure = fromActions.fetchProposalFailedAction();

const responseAccess = cold("-#", permission.accepted);
const outcome = fromActions.fetchProposalFailedAction();

proposalApi.proposalsControllerFindByIdAccess
.withArgs(proposalId)
.and.returnValue(responseAccess);
.and.returnValue(of(permission.accepted));
proposalApi.proposalsControllerFindById.and.returnValue(
throwError(() => new Error()),
);

actions = hot("a", { a: action });
const expected = cold("b", { b: failure });
const expected = cold("b", { b: outcome });

expect(effects.fetchProposal$).toBeObservable(expected);
});

it("should do nothing if findByIdAccess returns false", () => {
const action = fromActions.fetchProposalAction({ proposalId });

const responseAccess = cold("-#", permission.rejected);

proposalApi.proposalsControllerFindByIdAccess
.withArgs(proposalId)
.and.returnValue(responseAccess);
.and.returnValue(of(permission.rejected));

actions = hot("a", { a: action });
const expected = cold("------");
Expand All @@ -313,14 +313,14 @@ describe("ProposalEffects", () => {

it("should result in fetchProposalAccessFailedAction if findByIdAccess failed", () => {
const action = fromActions.fetchProposalAction({ proposalId });
const failure = fromActions.fetchProposalAccessFailedAction();
const outcome = fromActions.fetchProposalAccessFailedAction();

proposalApi.proposalsControllerFindByIdAccess
.withArgs(proposalId)
.and.returnValue(throwError(() => new Error()));

actions = hot("a", { a: action });
const expected = cold("b", { b: failure });
const expected = cold("b", { b: outcome });

expect(effects.fetchProposal$).toBeObservable(expected);
expect(proposalApi.proposalsControllerFindById).not.toHaveBeenCalled();
Expand Down
9 changes: 6 additions & 3 deletions src/app/state-management/effects/proposals.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export class ProposalEffects {
map(([action, params]) => params),
switchMap(({ query }) =>
this.proposalsService.proposalsControllerFullfacet(query).pipe(
map((proposals) =>
fromActions.fetchCountCompleteAction({ count: proposals.length }),
),
map((res) => {
const { all } = res[0];
const allCounts = all && all.length > 0 ? all[0].totalSets : 0;

return fromActions.fetchCountCompleteAction({ count: allCounts });
}),
catchError(() => of(fromActions.fetchCountFailedAction())),
),
),
Expand Down
22 changes: 8 additions & 14 deletions src/app/state-management/effects/samples.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { throwError } from "rxjs";
import { of, throwError } from "rxjs";
import { SampleEffects } from "./samples.effects";
import { TestBed } from "@angular/core/testing";
import { provideMockActions } from "@ngrx/effects/testing";
Expand Down Expand Up @@ -265,24 +265,22 @@ describe("SampleEffects", () => {

describe("fetchSample$", () => {
const sampleId = "testId";
// TODO: Try to fix the any types here
const permission = {
accepted: { canAccess: true },
rejected: { canAccess: false },
accepted: { canAccess: true } as any,
rejected: { canAccess: false } as any,
};

it("should result in a fetchSampleCompleteAction", () => {
const action = fromActions.fetchSampleAction({ sampleId });
const outcome = fromActions.fetchSampleCompleteAction({ sample });

const responseAccess = cold("-#", permission.accepted);
const responseSample = cold("-#", sample);

sampleApi.samplesControllerFindByIdAccess
.withArgs(sampleId)
.and.returnValue(responseAccess);
.and.returnValue(of(permission.accepted));
sampleApi.samplesControllerFindById
.withArgs(encodeURIComponent(sampleId))
.and.returnValue(responseSample);
.and.returnValue(of(sample as any));

actions = hot("a", { a: action });
const expected = cold("b", { b: outcome });
Expand All @@ -294,11 +292,9 @@ describe("SampleEffects", () => {
const action = fromActions.fetchSampleAction({ sampleId });
const failure = fromActions.fetchSampleFailedAction();

const responseAccess = cold("-#", permission.accepted);

sampleApi.samplesControllerFindByIdAccess
.withArgs(sampleId)
.and.returnValue(responseAccess);
.and.returnValue(of(permission.accepted));
sampleApi.samplesControllerFindById.and.returnValue(
throwError(() => new Error()),
);
Expand All @@ -312,11 +308,9 @@ describe("SampleEffects", () => {
it("should do nothing if findByIdAccess returns false", () => {
const action = fromActions.fetchSampleAction({ sampleId });

const responseAccess = cold("-#", permission.rejected);

sampleApi.samplesControllerFindByIdAccess
.withArgs(sampleId)
.and.returnValue(responseAccess);
.and.returnValue(of(permission.rejected));

actions = hot("a", { a: action });
const expected = cold("------");
Expand Down
Loading

0 comments on commit 37260d3

Please sign in to comment.