import { expect } from 'chai' ; import * as fs from 'fs' ; import * as path from 'path' ; import * as sinon from 'sinon' ; import * as vscode from 'vscode' ; import { CsvDataProvider , JsonDataProvider } from '../../../src/queryDataView/dataProviders' ; import { FileFormat } from '../../../src/queryDataView/queryDataFileService' ; import { mockQueryText , mockQueryData , MockTextDocumentProvider , TestFileService } from '../testUtilities' ; export const QUERY_RESULTS_DIR_PATH = path . join ( 'scripts' , 'soql' , 'query-results' ) ; describe ( 'Query Data File Service' , ( ) => { let mockTextDocument ; let docProviderDisposable ; const documentName = 'example.soql' ; const workspacePath = vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath ; const testResultsDirPath = path . join ( workspacePath , QUERY_RESULTS_DIR_PATH ) ; const mockUriPath = path . join ( testResultsDirPath , documentName ) ; let sandbox ; function createResultsDirectory ( ) { fs . mkdirSync ( testResultsDirPath , { recursive : true } ) ; } beforeEach ( async ( ) => { sandbox = sinon . createSandbox ( ) ; docProviderDisposable = vscode . workspace . registerTextDocumentContentProvider ( 'sfdc-test' , new MockTextDocumentProvider ( ) ) ; mockTextDocument = await vscode . workspace . openTextDocument ( vscode . Uri . parse ( 'sfdc-test:test/examples/soql/mocksoql.soql' ) ) ; createResultsDirectory ( ) ; } ) ; afterEach ( ( ) => { fs . rmdirSync ( testResultsDirPath , { recursive : true } ) ; sandbox . restore ( ) ; } ) ; it ( 'should use the correct data provider' , ( ) => { const csvFileService = new TestFileService ( mockQueryText , mockQueryData , FileFormat . CSV , mockTextDocument ) ; expect ( csvFileService . getDataProvider ( ) ) . instanceOf ( CsvDataProvider ) ; const jsonFileService = new TestFileService ( mockQueryText , mockQueryData , FileFormat . JSON , mockTextDocument ) ; expect ( jsonFileService . getDataProvider ( ) ) . instanceOf ( JsonDataProvider ) ; } ) ; it ( 'should save json file to disk on save' , async ( ) => { const jsonFileService = new TestFileService ( mockQueryText , mockQueryData , FileFormat . JSON , mockTextDocument ) ; const mockURI = { fsPath : mockUriPath } as vscode . Uri ; sandbox . stub ( vscode . window , 'showSaveDialog' ) . resolves ( mockURI ) ; const savedFilePath = await jsonFileService . save ( ) ; const savedFileContent = fs . readFileSync ( savedFilePath , 'utf8' ) ; expect ( JSON . parse ( savedFileContent ) ) . to . eql ( mockQueryData . records ) ; } ) ; it ( 'should save csv to file to disk on save' , async ( ) => { const csvFileService = new TestFileService ( mockQueryText , mockQueryData , FileFormat . CSV , mockTextDocument ) ; const mockURI = { fsPath : mockUriPath } as vscode . Uri ; sandbox . stub ( vscode . window , 'showSaveDialog' ) . resolves ( mockURI ) ; const savedFilePath = await csvFileService . save ( ) ; const savedFileContent = fs . readFileSync ( savedFilePath , 'utf8' ) ; const mockCsvData = csvFileService . getDataProvider ( ) . getFileContent ( mockQueryText , mockQueryData . records ) ; expect ( savedFileContent ) . to . equal ( mockCsvData ) ; } ) ; } ) ;