Skip to content

Commit 4081f0b

Browse files
committed
feat(frontend): preserve code color inversion
+ save color inversion of code in local storage + related unit tests
1 parent 6c65035 commit 4081f0b

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

frontend/src/app/app.consts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export enum ELocalStorageKey {
2222
* Last filters model
2323
*/
2424
CARD_FILTERS = 'cardholder-card-filters',
25+
/**
26+
* Wether to invert code colors by default
27+
*/
28+
CODE_COLOR_INVERSION = 'cardholder-code-color-inversion',
2529
}
2630
/**
2731
* Enum of useful regexp

frontend/src/app/shared/components/card-code-viewer/card-code-viewer.component.spec.ts

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { CardCodeViewerComponent } from './card-code-viewer.component';
3-
import { createMatDialogMock } from 'src/app/test';
4-
import { MatDialog } from '@angular/material/dialog';
2+
import {
3+
CardCodeViewerComponent,
4+
CardCodeViewerDialogComponent,
5+
ICardCodeViewerData,
6+
} from './card-code-viewer.component';
7+
import {
8+
createMatDialogMock,
9+
createMatDialogRefMock,
10+
TestTranslateModule,
11+
} from 'src/app/test';
12+
import {
13+
MAT_DIALOG_DATA,
14+
MatDialog,
15+
MatDialogRef,
16+
} from '@angular/material/dialog';
17+
import { ELocalStorageKey } from 'src/app/app.consts';
518

619
describe('CardCodeViewerComponent', () => {
720
let fixture: ComponentFixture<CardCodeViewerComponent>;
@@ -14,7 +27,6 @@ describe('CardCodeViewerComponent', () => {
1427

1528
await TestBed.configureTestingModule({
1629
providers: [{ provide: MatDialog, useValue: matDialogMock }],
17-
imports: [CardCodeViewerComponent],
1830
}).compileComponents();
1931
});
2032

@@ -74,3 +86,60 @@ describe('CardCodeViewerComponent', () => {
7486
expect(matDialogMock.open).toHaveBeenCalledTimes(1);
7587
});
7688
});
89+
90+
describe('CardCodeViewerDialogComponent', () => {
91+
let fixture: ComponentFixture<CardCodeViewerDialogComponent>;
92+
let component: CardCodeViewerDialogComponent;
93+
94+
let matDialogRefMock: ReturnType<typeof createMatDialogRefMock>;
95+
let matDialogDataMock: ICardCodeViewerData;
96+
97+
beforeEach(async () => {
98+
matDialogRefMock = createMatDialogRefMock();
99+
matDialogDataMock = {
100+
card: {
101+
code: '0123456789012',
102+
code_type: 'ean13',
103+
},
104+
scale: 3,
105+
color: 'black',
106+
};
107+
108+
await TestBed.configureTestingModule({
109+
imports: [TestTranslateModule],
110+
providers: [
111+
{ provide: MatDialogRef, useValue: matDialogRefMock },
112+
{ provide: MAT_DIALOG_DATA, useValue: matDialogDataMock },
113+
],
114+
}).compileComponents();
115+
});
116+
117+
it('should create', () => {
118+
fixture = TestBed.createComponent(CardCodeViewerDialogComponent);
119+
component = fixture.componentInstance;
120+
fixture.detectChanges();
121+
expect(component).toBeTruthy();
122+
});
123+
124+
it('should preserve color inversion in local storage', () => {
125+
const lsGetSpy = spyOn(localStorage, 'getItemJson').and.callFake(
126+
() => true as any,
127+
);
128+
const lsSetSpy = spyOn(localStorage, 'setItemJson').and.callFake(() => {});
129+
fixture = TestBed.createComponent(CardCodeViewerDialogComponent);
130+
component = fixture.componentInstance;
131+
fixture.detectChanges();
132+
133+
expect(lsGetSpy).toHaveBeenCalledTimes(1);
134+
expect(component.invert()).toBeTrue();
135+
136+
component.toggleInvert();
137+
138+
expect(lsSetSpy).toHaveBeenCalledTimes(1);
139+
expect(lsSetSpy).toHaveBeenCalledWith(
140+
ELocalStorageKey.CODE_COLOR_INVERSION,
141+
false,
142+
);
143+
expect(component.invert()).toBeFalse();
144+
});
145+
});

frontend/src/app/shared/components/card-code-viewer/card-code-viewer.component.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
OnChanges,
99
OnInit,
1010
Output,
11+
signal,
1112
SimpleChanges,
1213
ViewChild,
1314
} from '@angular/core';
@@ -23,6 +24,7 @@ import {
2324
import { MatIcon } from '@angular/material/icon';
2425
import Bwip from '@bwip-js/browser';
2526
import { TranslateModule } from '@ngx-translate/core';
27+
import { ELocalStorageKey } from 'src/app/app.consts';
2628
import { ZxingToBwipMap } from 'src/app/entities/cards/cards-const';
2729
import { ICardBase } from 'src/app/entities/cards/cards-interface';
2830

@@ -148,11 +150,11 @@ export class CardCodeViewerComponent
148150
<h2 mat-dialog-title>{{ card.name }}</h2>
149151
<mat-dialog-content
150152
class="mat-dialog-content"
151-
[class.invert]="invert">
153+
[class.invert]="invert()">
152154
<canvas
153155
class="canvas"
154156
#canvas
155-
(click)="invert = !invert">
157+
(click)="toggleInvert()">
156158
</canvas>
157159
@if (card.description) {
158160
<pre class="desc" [innerHTML]="card.description"></pre>
@@ -161,7 +163,7 @@ export class CardCodeViewerComponent
161163
<mat-dialog-actions class="mat-dialog-actions">
162164
<button
163165
mat-button
164-
(click)="invert = !invert">
166+
(click)="toggleInvert()">
165167
<mat-icon>touch_app</mat-icon>
166168
{{ 'CARDS.VIEWER.INVERT' | translate }}
167169
</button>
@@ -204,14 +206,22 @@ export class CardCodeViewerDialogComponent extends CardCodeViewerComponent {
204206
private readonly matDialogRef = inject(MatDialogRef);
205207
private readonly data: ICardCodeViewerData = inject(MAT_DIALOG_DATA);
206208

207-
public invert: boolean = false;
209+
public readonly invert = signal<boolean>(
210+
localStorage.getItemJson(ELocalStorageKey.CODE_COLOR_INVERSION),
211+
);
208212

209213
constructor() {
210214
super();
211215
Object.assign(this, this.data);
212216
}
213217

214-
public close() {
218+
public toggleInvert(): void {
219+
const invert = !this.invert();
220+
this.invert.set(invert);
221+
localStorage.setItemJson(ELocalStorageKey.CODE_COLOR_INVERSION, invert);
222+
}
223+
224+
public close(): void {
215225
this.matDialogRef.close();
216226
}
217227
}

0 commit comments

Comments
 (0)