Skip to content
Prev Previous commit
Next Next commit
refactor: TTL-887 english lessons and safari books can skip descripti…
…on and ticket number
  • Loading branch information
mmaquina committed May 31, 2023
commit 2c9a164f722260ac2a64601effcb549d98690203
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ describe('DetailsFieldsComponent', () => {
expect(toastrServiceStub.warning).toHaveBeenCalled();
});

it('if entry is set to project_name search_fiend is assigned in entryForm', () => {
it('if entry is set to project_name search_field is assigned in entryForm', () => {
const listProjects: Project[] = [{ id: 'id', name: 'abc', status: 'active', search_field: 'name' }];
component.listProjects = listProjects;
component.entryToEdit = { ...entryToEdit };
Expand Down Expand Up @@ -771,6 +771,20 @@ describe('DetailsFieldsComponent', () => {
expect(component.saveEntry.emit).toHaveBeenCalled();
});

/* We allow saving time entries with empty fields in uri and description for safari books and english lessons */
it('should not display a warning message when trying to save time entry of English Lessons without description and save', () => {
component.entryForm.setValue({ ...entryWithoutRequiredFields, project_name: 'ioet inc. - English Lessons' });

spyOn(toastrServiceStub, 'warning');
spyOn(component.saveEntry, 'emit');

component.onSubmit();
expect(toastrServiceStub.warning).not.toHaveBeenCalled();

expect(component.saveEntry.emit).toHaveBeenCalled();
});


/*
TODO As part of https://github.com/ioet/time-tracker-ui/issues/424 a new parameter was added to the details-field-component,
and now these couple of tests are failing. A solution to this error might be generate a Test Wrapper Component. More details here:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import { TechnologiesComponent } from '../technologies/technologies.component';
import { MatDatepicker } from '@angular/material/datepicker';
import { Observable } from 'rxjs';

const INTERNAL_APP_STRING = 'ioet';
const PROJECT_NAME_TO_SKIP = ['English Lessons', 'Safari Books'];
const EMPTY_FIELDS_ERROR_MESSAGE = 'Make sure to add a description and/or ticket number when working on an internal app.';

type Merged = TechnologyState & ProjectState & ActivityState & EntryState;
@Component({
selector: 'app-details-fields',
Expand Down Expand Up @@ -332,11 +336,13 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {

onSubmit() {

if (this.entryForm.value.project_name.includes('ioet')) {
if (this.entryForm.value.uri === '' && this.entryForm.value.description === '') {
this.toastrService.warning('Make sure to add a description and/or ticket number when working on an internal app');
return;
}
const emptyValue = '';
const { project_name, uri, description } = this.entryForm.value;
const areEmptyValues = [uri, description].every(item => item === emptyValue);
const canSkipDescriptionAndURI = PROJECT_NAME_TO_SKIP.some(projectNameItem => project_name.includes(projectNameItem));
if (project_name.includes(INTERNAL_APP_STRING) && areEmptyValues && !canSkipDescriptionAndURI) {
this.toastrService.warning(EMPTY_FIELDS_ERROR_MESSAGE);
return;
}

if (this.entryForm.invalid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,23 @@ describe('EntryFieldsComponent', () => {

it('should show an error message if description and ticket fields are empty for internal apps', () => {
spyOn(toastrServiceStub, 'error');
const result = component.requiredFieldsForInternalAppExist('ioet');
const result = component.requiredFieldsForInternalAppExist('ioet', 'project name');
expect(toastrServiceStub.error).toHaveBeenCalled();
expect(result).toBe(false);
});

it('should return true if customer name does not contain ioet ', () => {
spyOn(toastrServiceStub, 'error');
const result = component.requiredFieldsForInternalAppExist('Project Name');
const result = component.requiredFieldsForInternalAppExist('customer', 'Project Name');
expect(toastrServiceStub.error).not.toHaveBeenCalled();
expect(result).toBe(true);
});

it('should return true if customer name contain ioet and project name contain Safari Books', () => {
spyOn(toastrServiceStub, 'error');
const result = component.requiredFieldsForInternalAppExist('customer', 'Safari Books');
expect(toastrServiceStub.error).not.toHaveBeenCalled();
expect(result).toBe(true);
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { getTimeEntriesDataSource } from '../../store/entry.selectors';
import { DATE_FORMAT } from 'src/environments/environment';
import { Subscription } from 'rxjs';


const INTERNAL_APP_STRING = 'ioet';
const PROJECT_NAME_TO_SKIP = ['English Lessons', 'Safari Books'];
const EMPTY_FIELDS_ERROR_MESSAGE = 'Make sure to add a description and/or ticket number when working on an internal app.';

type Merged = TechnologyState & ProjectState & ActivityState;

@Component({
Expand Down Expand Up @@ -134,11 +139,14 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {

entryFormIsValidate() {
let customerName = '';
let projectName = '';
this.store.pipe(select(getTimeEntriesDataSource)).subscribe((ds) => {
const dataToUse = ds.data.find((item) => item.project_id === this.activeEntry.project_id);
customerName = dataToUse.customer_name;
projectName = dataToUse.project_name;

});
return this.requiredFieldsForInternalAppExist(customerName) && this.entryForm.valid;
return this.requiredFieldsForInternalAppExist(customerName, projectName) && this.entryForm.valid;
}

onSubmit() {
Expand Down Expand Up @@ -199,12 +207,15 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
this.actionSetDateSubscription.unsubscribe();
}

requiredFieldsForInternalAppExist(customerName) {
const emptyFields = this.entryForm.value.uri === '' && this.entryForm.value.description === '';
requiredFieldsForInternalAppExist(customerName, projectName) {
const emptyValue = '';
const areEmptyValues = [this.entryForm.value.uri, this.entryForm.value.description].every(item => item === emptyValue);

const isInternalApp = customerName.includes('ioet');
if (isInternalApp && emptyFields) {
const message = 'The description field or ticket field should not be empty';
this.toastrService.error(`Some fields are empty, ${message}.`);
const canSkipDescriptionAndURI = PROJECT_NAME_TO_SKIP.some(projectNameItem => projectName.includes(projectNameItem));

if (isInternalApp && areEmptyValues && !canSkipDescriptionAndURI) {
this.toastrService.error(EMPTY_FIELDS_ERROR_MESSAGE);
return false;
}
return true;
Expand Down