Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(viewer): make hex code display optional for color value (DSP-1667) #303

Merged
merged 3 commits into from Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,8 +1,7 @@
<span *ngIf="mode === 'read'; else showForm" class="read-mode-view">
<div class="color-preview"
[style.color]="textColor"
<div class="rm-value"
[style.background-color]="valueFormControl.value">
<span class="rm-value">{{ valueFormControl.value }}</span>
<span *ngIf="showHexCode" class="hexcode" [style.color]="textColor">{{ valueFormControl.value }}</span>
</div>
<span class="rm-comment" *ngIf="shouldShowComment">
{{ commentFormControl.value }}
Expand Down
Expand Up @@ -15,10 +15,11 @@
}
}

.color-preview {
.rm-value {
border: 1px solid rgba(33,33,33,.5);
border-radius: 4px;
padding: 3px 12px;
margin: 6px 0;
margin: 6px 0 10px 0 !important;
width: 6em;
min-height: 8px;
}
Expand Up @@ -70,7 +70,7 @@ class TestColorPickerComponent implements ControlValueAccessor, MatFormFieldCont
*/
@Component({
template: `
<dsp-color-value #colorVal [displayValue]="displayColorVal" [mode]="mode"></dsp-color-value>`
<dsp-color-value #colorVal [displayValue]="displayColorVal" [mode]="mode" [showHexCode]="showColorHex"></dsp-color-value>`
})
class TestHostDisplayValueComponent implements OnInit {

Expand All @@ -80,6 +80,8 @@ class TestHostDisplayValueComponent implements OnInit {

mode: 'read' | 'update' | 'create' | 'search';

showColorHex = false;

ngOnInit() {

MockResource.getTestThing().subscribe(res => {
Expand Down Expand Up @@ -159,15 +161,35 @@ describe('ColorValueComponent', () => {
valueReadModeNativeElement = valueReadModeDebugElement.nativeElement;
});

it('should display an existing value', () => {
it('should display an existing value without a hex color code', () => {

expect(testHostComponent.colorValueComponent.displayValue.color).toEqual('#ff3333');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

expect(testHostComponent.colorValueComponent.mode).toEqual('read');

expect(valueReadModeNativeElement.innerText).toEqual('#ff3333');
expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('');

});

it('should display an existing value with a hex color code', () => {

testHostComponent.showColorHex = true;

testHostFixture.detectChanges();

expect(testHostComponent.colorValueComponent.displayValue.color).toEqual('#ff3333');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

expect(testHostComponent.colorValueComponent.mode).toEqual('read');

expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('#ff3333');

});

Expand Down Expand Up @@ -301,12 +323,54 @@ describe('ColorValueComponent', () => {

testHostFixture.detectChanges();

expect(valueReadModeNativeElement.innerText).toEqual('#d8d8d8');
expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

});

it('should set a new display value not showing the hex color code', () => {

const newColor = new ReadColorValue();

newColor.color = '#d8d8d8';
newColor.id = 'updatedId';

testHostComponent.displayColorVal = newColor;

testHostFixture.detectChanges();

expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

});

it('should set a new display value showing the hex color code', () => {

testHostComponent.showColorHex = true;

const newColor = new ReadColorValue();

newColor.color = '#d8d8d8';
newColor.id = 'updatedId';

testHostComponent.displayColorVal = newColor;

testHostFixture.detectChanges();

expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('#d8d8d8');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

});

it('should unsubscribe when destroyed', () => {
expect(testHostComponent.colorValueComponent.valueChangesSubscription.closed).toBeFalsy();

Expand Down
Expand Up @@ -21,6 +21,8 @@ export class ColorValueComponent extends BaseValueComponent implements OnInit, O

@Input() displayValue?: ReadColorValue;

@Input() showHexCode = false;

valueFormControl: FormControl;
commentFormControl: FormControl;
form: FormGroup;
Expand Down Expand Up @@ -71,6 +73,10 @@ export class ColorValueComponent extends BaseValueComponent implements OnInit, O

ngOnChanges(changes: SimpleChanges): void {
this.resetFormControl();

if (this.showHexCode && this.valueFormControl !== undefined) {
this.textColor = this.getTextColor(this.valueFormControl.value);
}
}

// unsubscribe when the object is destroyed to prevent memory leaks
Expand Down