NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import ContainerValidationSetup from '../stores/ContainerValidationSetup';
import appState from '../stores/appState';
import gatewayApi from '../api/gatewayApi';
import api from '../api/api';
import fileValidatorMain from '../utils/import/fileValidators';
import fileValidator from '../utils/import/fileValidatorWebWorkerWrapper';
import dataValidators from '../utils/import/dataValidatorWebWorkerWrapper';
import columnDefinitionsRoc from '../utils/import/columnDefinitionsRoc';
import Constants from '../constants/AppConstants';

jest.mock('../stores/appState');
jest.mock('../api/gatewayApi');
jest.mock('../api/api');
jest.mock('../utils/import/fileValidators');
jest.mock('../utils/import/fileValidatorWebWorkerWrapper');
jest.mock('../utils/import/dataValidatorWebWorkerWrapper');
jest.mock('../utils/import/columnDefinitionsRoc');

describe('ContainerValidationSetup', () => {
let containerValidationSetup;

beforeEach(() => {
jest.resetAllMocks();
containerValidationSetup = new ContainerValidationSetup();
containerValidationSetup.appState = appState;
});

describe('updateValue', () => {
it('should update the specified value', () => {
containerValidationSetup.updateValue({
key: 'file',
value: 'example.csv'
});
expect(containerValidationSetup.file).toBe('example.csv');
});
});

describe('resetState', () => {
it('should reset the state of the containerValidationSetup', () => {
containerValidationSetup.file = 'example.csv';
containerValidationSetup.isProcessing = true;
containerValidationSetup.displaySummary = true;
containerValidationSetup.summary = { valid: 10, invalid: 5 };
containerValidationSetup.tableData = [{ id: 1 }, { id: 2 }];
containerValidationSetup.validationErrors = ['Error 1', 'Error 2'];
containerValidationSetup.revision = 'revision';
containerValidationSetup.revisionOptions = [{ value: 'rev1', label: 'Revision 1' }];
containerValidationSetup.tableFilterOptions = { status: ['valid', 'invalid'], level: [1, 2] };
containerValidationSetup.isExporting = true;

containerValidationSetup.resetState();

expect(containerValidationSetup.file).toBeNull();
expect(containerValidationSetup.isProcessing).toBeFalsy();
expect(containerValidationSetup.displaySummary).toBeFalsy();
expect(containerValidationSetup.summary).toEqual({});
expect(containerValidationSetup.tableData).toEqual([]);
expect(containerValidationSetup.validationErrors).toEqual([]);
expect(containerValidationSetup.revision).toBeNull();
expect(containerValidationSetup.revisionOptions).toEqual([]);
expect(containerValidationSetup.tableFilterOptions).toEqual({});
expect(containerValidationSetup.isExporting).toBeFalsy();
});
});

describe('initialize', () => {
it('should initialize the containerValidationSetup', async () => {
gatewayApi.fetchRevisionsAll.mockResolvedValue({ data: ['rev1', 'rev2'] });
api.getRevisionsForContainerParts.mockResolvedValue({ data: ['rev2'] });

await containerValidationSetup.initialize('part1,part2');

expect(gatewayApi.fetchRevisionsAll).toHaveBeenCalledTimes(1);
expect(api.getRevisionsForContainerParts).toHaveBeenCalledWith({ containerParts: 'part1,part2' });
expect(containerValidationSetup.revisionOptions).toEqual([
{ value: 'rev1', label: 'rev1' },
{ value: 'rev2', label: 'rev2' }
]);
expect(containerValidationSetup.revision).toBeNull();
expect(appState.isInitializing).toBeFalsy();
});

it('should handle errors during initialization', async () => {
gatewayApi.fetchRevisionsAll.mockRejectedValue(new Error('Failed to fetch revisions'));

await containerValidationSetup.initialize('part1,part2');

expect(gatewayApi.fetchRevisionsAll).toHaveBeenCalledTimes(1);
expect(api.getRevisionsForContainerParts).not.toHaveBeenCalled();
expect(containerValidationSetup.revisionOptions).toEqual([]);
expect(containerValidationSetup.revision).toBeNull();
expect(appState.isInitializing).toBeFalsy();
expect(appState.handleNotification).toHaveBeenCalledWith({ message: 'Failed initializing page.' });
});
});

describe('validateFile', () => {
it('should validate the imported file', async () => {
const files = ['example.csv'];
const rawTextObjects = ['raw text'];

fileValidatorMain.validateFiles.mockResolvedValue(files);
fileValidator.convertFileToText.mockResolvedValue(rawTextObjects);
dataValidators.parseRawText.mockResolvedValue({ meta: { fields: ['field1', 'field2'] } });
dataValidators.convertToRowObjects.mockResolvedValue([{ id: 1 }, { id: 2 }]);
dataValidators.validateColumnsRoc.mockResolvedValue([]);

await containerValidationSetup.validateFile(files);

expect(fileValidatorMain.validateFiles).toHaveBeenCalledWith(files);
expect(fileValidator.convertFileToText).toHaveBeenCalledWith(files[0]);
expect(dataValidators.parseRawText).toHaveBeenCalledWith(rawTextObjects);
expect(dataValidators.convertToRowObjects).toHaveBeenCalledWith(
{ meta: { fields: ['field1', 'field2'] } },
rawTextObjects
);
expect(dataValidators.validateColumnsRoc).toHaveBeenCalledWith(
['field1', 'field2'],
columnDefinitionsRoc,
files[0]
);
expect(containerValidationSetup.file).toBe(files[0]);
expect(containerValidationSetup.validationErrors).toEqual([]);
expect(containerValidationSetup.isProcessing).toBeFalsy();
});

it('should handle errors during file validation', async () => {
fileValidatorMain.validateFiles.mockRejectedValue(new Error('Failed to validate file'));

await containerValidationSetup.validateFile(['example.csv']);

expect(fileValidatorMain.validateFiles).toHaveBeenCalledTimes(1);
expect(containerValidationSetup.file).toBeNull();
expect(containerValidationSetup.validationErrors).toEqual([]);
expect(containerValidationSetup.isProcessing).toBeFalsy();
expect(appState.handleNotification).toHaveBeenCalledWith({ message: 'Failed to validate file' });
});
});

describe('validateContainerParts', () => {
beforeEach(() => {
containerValidationSetup.revision = { value: 'rev1' };
containerValidationSetup.isValidatingByFeed = true;
containerValidationSetup.file = 'example.csv';
});

it('should validate container parts by feed', async () => {
const data = {
summary: { valid: 10, invalid: 5 },
revision: 'rev1',
validations: [{ id: 1 }, { id: 2 }]
};

api.validateContainerPartsByFeed.mockResolvedValue({ data });

await containerValidationSetup.validateContainerParts('part1,part2');

expect(api.validateContainerPartsByFeed).toHaveBeenCalledWith({
containerParts: 'part1,part2',
revisionCode: 'rev1'
});
expect(containerValidationSetup.summary).toEqual(data.summary);
expect(containerValidationSetup.tableData).toEqual(data.validations);
expect(containerValidationSetup.displaySummary).toBeTruthy();
expect(containerValidationSetup.appState.isFetching).toBeFalsy();
});

it('should validate container parts by file', async () => {
const data = {
summary: { valid: 10, invalid: 5 },
revision: 'rev1',
validations: [{ id: 1 }, { id: 2 }]
};

api.validateContainerPartsByFile.mockResolvedValue({ data });

await containerValidationSetup.validateContainerParts('part1,part2');

expect(api.validateContainerPartsByFile).toHaveBeenCalledWith(
{ containerParts: 'part1,part2', revisionCode: 'rev1' },
expect.any(FormData)
);
expect(containerValidationSetup.summary).toEqual(data.summary);
expect(containerValidationSetup.tableData).toEqual(data.validations);
expect(containerValidationSetup.displaySummary).toBeTruthy();
expect(containerValidationSetup.appState.isFetching).toBeFalsy();
});

it('should handle errors during container parts validation', async () => {
api.validateContainerPartsByFeed.mockRejectedValue(new Error('Failed to validate container parts'));

await containerValidationSetup.validateContainerParts('part1,part2');

expect(api.validateContainerPartsByFeed).toHaveBeenCalledTimes(1);
expect(containerValidationSetup.summary).toEqual({});
expect(containerValidationSetup.tableData).toEqual([]);
expect(containerValidationSetup.displaySummary).toBeFalsy();
expect(containerValidationSetup.appState.isFetching).toBeFalsy();
expect(appState.handleNotification).toHaveBeenCalledWith({
message: 'Data import failed. Please retry.'
});
});
});

describe('exportTable', () => {
it('should export the table data', async () => {
const filteredData = [{ id: 1 }, { id: 2 }];

api.exportContainerValidationSetup.mockResolvedValue({ status: 200, data: 'link' });

await containerValidationSetup.exportTable(filteredData);

expect(api.exportContainerValidationSetup).toHaveBeenCalledWith({ validations: filteredData });
expect(appState.handleNotification).toHaveBeenCalledWith({
message: 'Successfully exported 2 containers. Check your email to access a temporary Box link and password to download the file(s). You can find the link using: link',
levelType: 'success'
});
expect(containerValidationSetup.isExporting).toBeFalsy();
});

it('should handle no data to export', async () => {
const filteredData = [];

api.exportContainerValidationSetup.mockResolvedValue({ status: 204 });

await containerValidationSetup.exportTable(filteredData);

expect(api.exportContainerValidationSetup).toHaveBeenCalledWith({ validations: filteredData });
expect(appState.handleNotification).toHaveBeenCalledWith({
message: 'No data to export',
levelType: 'warning'
});
expect(containerValidationSetup.isExporting).toBeFalsy();
});

it('should handle errors during export', async () => {
const filteredData = [{ id: 1 }, { id: 2 }];

api.exportContainerValidationSetup.mockRejectedValue(new Error('Failed to export'));

await containerValidationSetup.exportTable(filteredData);

expect(api.exportContainerValidationSetup).toHaveBeenCalledWith({ validations: filteredData });
expect(appState.handleNotification).toHaveBeenCalledWith({
error: new Error('Failed to export'),
message: 'Failed to export. Please try again.',
levelType: 'danger'
});
expect(containerValidationSetup.isExporting).toBeFalsy();
});
});
});
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.