Skip to content

Commit 3d4f77a

Browse files
authored
Merge branch 'master' into dependabot/npm_and_yarn/ngx-cookie-service-20.0.1
2 parents 7a128fd + a995ede commit 3d4f77a

File tree

2 files changed

+321
-1
lines changed

2 files changed

+321
-1
lines changed

src/app/datasets/dataset-table/dataset-table.component.spec.ts

Lines changed: 285 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from "state-management/actions/datasets.actions";
3030
import { provideMockStore } from "@ngrx/store/testing";
3131
import { selectDatasets } from "state-management/selectors/datasets.selectors";
32+
import { selectInstruments } from "state-management/selectors/instruments.selectors";
3233
import { MatTableModule } from "@angular/material/table";
3334
import {
3435
MatCheckboxChange,
@@ -73,7 +74,10 @@ describe("DatasetTableComponent", () => {
7374
],
7475
providers: [
7576
provideMockStore({
76-
selectors: [{ selector: selectDatasets, value: [] }],
77+
selectors: [
78+
{ selector: selectDatasets, value: [] },
79+
{ selector: selectInstruments, value: [] },
80+
],
7781
}),
7882
JsonHeadPipe,
7983
DatePipe,
@@ -388,4 +392,284 @@ describe("DatasetTableComponent", () => {
388392
);
389393
});
390394
});
395+
396+
describe("#convertSavedColumns() with instrumentName", () => {
397+
beforeEach(() => {
398+
component.instruments = [
399+
{
400+
pid: "instrument1",
401+
uniqueName: "unique1",
402+
name: "Test Instrument 1",
403+
},
404+
{
405+
pid: "instrument2",
406+
uniqueName: "unique2",
407+
name: "Test Instrument 2",
408+
},
409+
{ pid: "instrument3", uniqueName: "unique3", name: "" },
410+
] as any[];
411+
412+
component.instrumentMap = new Map(
413+
component.instruments.map((instrument) => [instrument.pid, instrument]),
414+
);
415+
});
416+
417+
it("should render instrument name when instrument is found", () => {
418+
const columns = [
419+
{
420+
name: "instrumentName",
421+
order: 0,
422+
enabled: true,
423+
width: 200,
424+
type: "standard" as const,
425+
},
426+
];
427+
428+
const convertedColumns = component.convertSavedColumns(columns);
429+
const instrumentColumn = convertedColumns[0];
430+
431+
const mockRow = { instrumentId: "instrument1" };
432+
const result = instrumentColumn.customRender(instrumentColumn, mockRow);
433+
434+
expect(result).toBe("Test Instrument 1");
435+
});
436+
437+
it("should render instrumentId when instrument is not found", () => {
438+
const columns = [
439+
{
440+
name: "instrumentName",
441+
order: 0,
442+
enabled: true,
443+
width: 200,
444+
type: "standard" as const,
445+
},
446+
];
447+
448+
const convertedColumns = component.convertSavedColumns(columns);
449+
const instrumentColumn = convertedColumns[0];
450+
451+
const mockRow = { instrumentId: "nonexistent" };
452+
const result = instrumentColumn.customRender(instrumentColumn, mockRow);
453+
454+
expect(result).toBe("nonexistent");
455+
});
456+
457+
it("should render '-' when instrumentId is not present", () => {
458+
const columns = [
459+
{
460+
name: "instrumentName",
461+
order: 0,
462+
enabled: true,
463+
width: 200,
464+
type: "standard" as const,
465+
},
466+
];
467+
468+
const convertedColumns = component.convertSavedColumns(columns);
469+
const instrumentColumn = convertedColumns[0];
470+
471+
const mockRow = {};
472+
const result = instrumentColumn.customRender(instrumentColumn, mockRow);
473+
474+
expect(result).toBe("-");
475+
});
476+
477+
it("should render instrumentId when instrument has empty name", () => {
478+
const columns = [
479+
{
480+
name: "instrumentName",
481+
order: 0,
482+
enabled: true,
483+
width: 200,
484+
type: "standard" as const,
485+
},
486+
];
487+
488+
const convertedColumns = component.convertSavedColumns(columns);
489+
const instrumentColumn = convertedColumns[0];
490+
491+
const mockRow = { instrumentId: "instrument3" };
492+
const result = instrumentColumn.customRender(instrumentColumn, mockRow);
493+
494+
expect(result).toBe("instrument3");
495+
});
496+
497+
it("should export instrument name when instrument is found", () => {
498+
const columns = [
499+
{
500+
name: "instrumentName",
501+
order: 0,
502+
enabled: true,
503+
width: 200,
504+
type: "standard" as const,
505+
},
506+
];
507+
508+
const convertedColumns = component.convertSavedColumns(columns);
509+
const instrumentColumn = convertedColumns[0];
510+
511+
const mockRow = { instrumentId: "instrument2" };
512+
const result = instrumentColumn.toExport(mockRow, instrumentColumn);
513+
514+
expect(result).toBe("Test Instrument 2");
515+
});
516+
517+
it("should export instrumentId when instrument is not found", () => {
518+
const columns = [
519+
{
520+
name: "instrumentName",
521+
order: 0,
522+
enabled: true,
523+
width: 200,
524+
type: "standard" as const,
525+
},
526+
];
527+
528+
const convertedColumns = component.convertSavedColumns(columns);
529+
const instrumentColumn = convertedColumns[0];
530+
531+
const mockRow = { instrumentId: "unknown-instrument" };
532+
const result = instrumentColumn.toExport(mockRow, instrumentColumn);
533+
534+
expect(result).toBe("unknown-instrument");
535+
});
536+
537+
it("should not affect other column types", () => {
538+
const columns = [
539+
{
540+
name: "datasetName",
541+
order: 0,
542+
enabled: true,
543+
width: 200,
544+
type: "standard" as const,
545+
},
546+
{
547+
name: "instrumentName",
548+
order: 1,
549+
enabled: true,
550+
width: 200,
551+
type: "standard" as const,
552+
},
553+
];
554+
555+
const convertedColumns = component.convertSavedColumns(columns);
556+
557+
expect(convertedColumns.length).toBe(2);
558+
expect(convertedColumns[0].name).toBe("datasetName");
559+
expect(convertedColumns[0].customRender).toBeUndefined();
560+
expect(convertedColumns[1].name).toBe("instrumentName");
561+
expect(convertedColumns[1].customRender).toBeDefined();
562+
});
563+
});
564+
565+
describe("instruments subscription with Map optimization", () => {
566+
it("should update both instruments array and instrumentMap when instruments observable changes", () => {
567+
const mockInstruments = [
568+
{ pid: "inst1", uniqueName: "unique1", name: "Instrument 1" },
569+
{ pid: "inst2", uniqueName: "unique2", name: "Instrument 2" },
570+
];
571+
572+
component.instruments = mockInstruments;
573+
component.instrumentMap = new Map(
574+
mockInstruments.map((instrument) => [instrument.pid, instrument]),
575+
);
576+
577+
expect(component.instruments).toEqual(mockInstruments);
578+
expect(component.instrumentMap.size).toBe(2);
579+
expect(component.instrumentMap.get("inst1")).toEqual(mockInstruments[0]);
580+
expect(component.instrumentMap.get("inst2")).toEqual(mockInstruments[1]);
581+
});
582+
583+
it("should handle empty instruments array and clear instrumentMap", () => {
584+
component.instruments = [];
585+
component.instrumentMap = new Map();
586+
587+
expect(component.instruments).toEqual([]);
588+
expect(component.instrumentMap.size).toBe(0);
589+
});
590+
591+
it("should provide O(1) lookup performance for instrument retrieval", () => {
592+
const mockInstruments = [
593+
{ pid: "fast-lookup", uniqueName: "unique1", name: "Fast Instrument" },
594+
];
595+
596+
component.instrumentMap = new Map(
597+
mockInstruments.map((instrument) => [instrument.pid, instrument]),
598+
);
599+
600+
const foundInstrument = component.instrumentMap.get("fast-lookup");
601+
expect(foundInstrument).toEqual(mockInstruments[0]);
602+
603+
const notFoundInstrument = component.instrumentMap.get("nonexistent");
604+
expect(notFoundInstrument).toBeUndefined();
605+
});
606+
});
607+
608+
describe("#getInstrumentName() private method", () => {
609+
beforeEach(() => {
610+
const mockInstruments = [
611+
{ pid: "inst1", uniqueName: "unique1", name: "Test Instrument 1" },
612+
{ pid: "inst2", uniqueName: "unique2", name: "Test Instrument 2" },
613+
{ pid: "inst3", uniqueName: "unique3", name: "" },
614+
];
615+
616+
component.instrumentMap = new Map(
617+
mockInstruments.map((instrument) => [instrument.pid, instrument]),
618+
);
619+
});
620+
621+
it("should return instrument name when instrument is found", () => {
622+
const mockRow = { instrumentId: "inst1" } as any;
623+
const result = component["getInstrumentName"](mockRow);
624+
expect(result).toBe("Test Instrument 1");
625+
});
626+
627+
it("should return instrumentId when instrument is not found", () => {
628+
const mockRow = { instrumentId: "nonexistent" } as any;
629+
const result = component["getInstrumentName"](mockRow);
630+
expect(result).toBe("nonexistent");
631+
});
632+
633+
it("should return '-' when instrumentId is not present", () => {
634+
const mockRow = {} as any;
635+
const result = component["getInstrumentName"](mockRow);
636+
expect(result).toBe("-");
637+
});
638+
639+
it("should return instrumentId when instrument has empty name", () => {
640+
const mockRow = { instrumentId: "inst3" } as any;
641+
const result = component["getInstrumentName"](mockRow);
642+
expect(result).toBe("inst3");
643+
});
644+
645+
it("should handle undefined instrumentId gracefully", () => {
646+
const mockRow = { instrumentId: undefined } as any;
647+
const result = component["getInstrumentName"](mockRow);
648+
expect(result).toBe("-");
649+
});
650+
651+
it("should handle null instrumentId gracefully", () => {
652+
const mockRow = { instrumentId: null } as any;
653+
const result = component["getInstrumentName"](mockRow);
654+
expect(result).toBe("-");
655+
});
656+
657+
it("should handle empty string instrumentId gracefully", () => {
658+
const mockRow = { instrumentId: "" } as any;
659+
const result = component["getInstrumentName"](mockRow);
660+
expect(result).toBe("-");
661+
});
662+
663+
it("should return instrument name even when instrumentId is empty but instrument exists", () => {
664+
// Add an instrument with empty string pid to test edge case
665+
component.instrumentMap.set("", {
666+
pid: "",
667+
name: "Empty PID Instrument",
668+
} as any);
669+
670+
const mockRow = { instrumentId: "" } as any;
671+
const result = component["getInstrumentName"](mockRow);
672+
expect(result).toBe("Empty PID Instrument");
673+
});
674+
});
391675
});

0 commit comments

Comments
 (0)