Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('DetailsFieldsComponent', () => {
projects: {
projects: [{ id: 'id', name: 'name', project_type_id: '', customer: { name: 'Juan', description: 'sadsa' } }],
customerProjects: [{ id: 'id', name: 'name', description: 'description', project_type_id: '123' }],
recentProjects: [{ id: 'id', name: 'name', customer: { name: 'Juan'} }],
recentProjects: [{ id: 'id', name: 'name', customer: { name: 'Juan' } }],
isLoading: false,
message: '',
projectToEdit: undefined,
Expand Down Expand Up @@ -104,6 +104,19 @@ describe('DetailsFieldsComponent', () => {

const mockCurrentDate = '2020-12-01T12:00:00';

const entryWithoutRequiredFields = {
project_id: 'p1',
project_name: 'ioet inc.',
activity_id: 'a1',
uri: '',
start_date: '2020-02-05',
end_date: '2020-02-05',
start_hour: '00:00',
end_hour: '00:01',
description: '',
technology: '',
};

beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -156,15 +169,15 @@ describe('DetailsFieldsComponent', () => {
});

it('onClearedComponent project id and name are set to empty', () => {
const search = {term: ''};
const search = { term: '' };
component.onClearedComponent(search);

expect(component.project_id.value).toBe('');
expect(component.project_name.value).toBe('');
});

it('should change the listProjectsShowed to listProjects if search is not empty on onClearedComponent', () => {
const search = {term: 'Ioet Inc.'};
const search = { term: 'Ioet Inc.' };
const listProjects: Project[] = [{ id: '1', name: 'abc', status: 'active' }];
component.listProjects = listProjects;
component.onClearedComponent(search);
Expand Down Expand Up @@ -708,14 +721,65 @@ describe('DetailsFieldsComponent', () => {
expect(toastrServiceStub.warning).toHaveBeenCalled();
});

it('if entry is set to project_name search_fiend is assigned in entryForm', () => {
const listProjects: Project[] = [{ id: 'id', name: 'abc', status: 'active', search_field: 'name'}];
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 };
component.ngOnChanges();
expect(component.entryForm.value.project_name).toBe('name');
});

it('should display a warning message when trying to save time entry of internal app without required fields', () => {
component.entryForm.setValue(entryWithoutRequiredFields);
spyOn(toastrServiceStub, 'warning');

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

it('should not display a warning message when trying to save time entry of internal app with uri and save', () => {
component.entryForm.setValue({ ...entryWithoutRequiredFields, uri: 'TTL-886' });
spyOn(toastrServiceStub, 'warning');
spyOn(component.saveEntry, 'emit');

component.onSubmit();
expect(toastrServiceStub.warning).not.toHaveBeenCalled();
expect(component.saveEntry.emit).toHaveBeenCalled();
});

it('should not display a warning message when trying to save time entry of external customer without required fields and save', () => {
component.entryForm.setValue({ ...entryWithoutRequiredFields, project_name: 'Warby Parker' });
spyOn(component.saveEntry, 'emit');
spyOn(toastrServiceStub, 'warning');

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

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

it('should not display a warning message when trying to save time entry of internal app with description and save', () => {
component.entryForm.setValue({ ...entryWithoutRequiredFields, description: 'Description' });
spyOn(component.saveEntry, 'emit');
spyOn(toastrServiceStub, 'warning');

component.onSubmit();
expect(toastrServiceStub.warning).not.toHaveBeenCalled();
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 @@ -27,6 +27,8 @@ import { DATE_FORMAT, DATE_FORMAT_YEAR } from 'src/environments/environment';
import { TechnologiesComponent } from '../technologies/technologies.component';
import { MatDatepicker } from '@angular/material/datepicker';
import { Observable } from 'rxjs';
import { EMPTY_FIELDS_ERROR_MESSAGE } from '../../messages';
import { INTERNAL_APP_STRING, PROJECT_NAME_TO_SKIP } from 'src/app/modules/shared/internal-app-constants';

type Merged = TechnologyState & ProjectState & ActivityState & EntryState;
@Component({
Expand Down Expand Up @@ -118,8 +120,8 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
});
}

onClearedComponent({term}) {
const isSearchEmpty = (term === '');
onClearedComponent({ term }) {
const isSearchEmpty = term === '';
if (isSearchEmpty) {
this.isTechnologiesDisabled = true;
this.entryForm.patchValue({
Expand Down Expand Up @@ -168,7 +170,7 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
projectWithSearchField.search_field = `${project.customer.name} - ${project.name}`;
this.listRecentProjects.push(projectWithSearchField);
});
}else{
} else {
this.listRecentProjects = this.listProjects;
}
this.listProjectsShowed = this.listRecentProjects;
Expand Down Expand Up @@ -331,6 +333,15 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
}

onSubmit() {
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) {
this.toastrService.warning('Make sure to select a project and activity');
return;
Expand Down
2 changes: 2 additions & 0 deletions src/app/modules/shared/internal-app-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const INTERNAL_APP_STRING = 'ioet';
export const PROJECT_NAME_TO_SKIP = ['English Lessons', 'Safari Books'];
1 change: 1 addition & 0 deletions src/app/modules/shared/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const INFO_SAVED_SUCCESSFULLY = 'The data has been saved successfully';
export const INFO_DELETE_SUCCESSFULLY = 'The data has been deleted successfully';
export const UNEXPECTED_ERROR = 'An unexpected error happened, please try again later';
export const PROJECT_DEACTIVATED_SUCCESSFULLY = 'The project has been inactivated successfully';
export const EMPTY_FIELDS_ERROR_MESSAGE = 'Make sure to add a description and/or ticket number when working on an internal app.';
Loading