UNPKG

gsweet

Version:

Help with writing scripts to run against gSuite

264 lines (217 loc) 8.15 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: drive/driveOps.test.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: drive/driveOps.test.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** @module Drive/driveOps.test */ const chai = require('chai') chai.should() const driveOps = require('./driveOps') const logger = require('../utils/logger') /** * Fake the service used by the Google Drive API. We will just send back * the q, pageSize and fields params that are constructed and passed in * the code normally returns an array of objects that have a lot of properties * but we mostly care about id, name and mimeType */ const fakeDriveService = { files: { list: async ({q, pageSize, fields}) => { // console.log(q, pageSize, fields); fakeDriveService.q = q fakeDriveService.pageSize = pageSize fakeDriveService.fields = fields // todo based on q return ({ data: { files: [{ id: 'fakeId', name: 'fakeName', mimeType: mimeType.getType(mimeType.SPREADSHEET), }], nextPageToken: 'more data available', }, }) }, }, } const throwDriveService = { files: { list: async () => { const googleErrObj = { response: {data: {error: {errors: ['the fake error']}}}, } throw googleErrObj }, }, } const fakeTwoFiles = { files: { list: async () => ({ data: { files: [{id: 'fakeId1'}, {id: 'fakeId2'}], }, }), }, } const {mimeType} = driveOps beforeEach(() => { driveOps.init(fakeDriveService) logger.level = 'info' }) afterEach(() => { logger.level = 'debug' }) describe('driveOps module', () => { describe('mimeType enum should', () => { it('should have expected enum values', () => { const {mimeType} = driveOps mimeType.FOLDER.should.equal(mimeType.FOLDER) mimeType.FILE.should.equal(mimeType.FILE) mimeType.SPREADSHEET.should.equal(mimeType.SPREADSHEET) mimeType.DOC.should.equal(mimeType.DOC) }) it('should have a get function that returns element of properties property', () => { const {mimeType} = driveOps console.log(mimeType.getType(1)) mimeType.getType(mimeType.FOLDER).should.contain('folder') mimeType.getType(mimeType.FILE).should.contain('N/A') mimeType.getType(mimeType.SPREADSHEET).should.contain('spreadsheet') mimeType.getType(mimeType.DOC).should.contain('document') }) }) describe('getFile() should', () => { it('return file with expected name ', async () => { const result = await driveOps.getFile({withName: 'anyName'}) logger.debug(result) result.name.should.contain('fakeName') }) it('should throw when more than one file ', async () => { driveOps.init(fakeTwoFiles) await driveOps.getFile({withName: 'anyName'}) .catch(err => { err.should.equal('Found 2 files.') }) }) }) describe('getFileId() should', () => { it('return just the expected id', async () => { const result = await driveOps.getFileId({withName: 'fakeName', exactMatch: true}) result.should.equal('fakeId') }) }) describe('getFiles() should', () => { it('not have contains clause with exactMatch:true', async () => { await driveOps.getFiles({withName: 'myName', exactMatch: true}) fakeDriveService.q.should.not.contain('contains') }) it('throw custom string when files.list throws', async () => { let caught = false driveOps.init(throwDriveService) await driveOps.getFiles({withName: 'anything', exactMatch: undefined}) .catch(err => { caught = true err.should.match(/For anything.*the fake error/) }) caught.should.be.true }) }) describe('getFilesInFolder() should', () => { it('return filename and specified mimeType clause when not FOLDER', async () => { await driveOps.getFilesInFolder({ withFolderId: 'anyFolderId', ofType: mimeType.SPREADSHEET, }) const query = fakeDriveService.q const clause = new RegExp(`anyFolderId.+ mimeType = \\'${mimeType.getType(mimeType.SPREADSHEET)}`) fakeDriveService.fields.should.contain('mimeType') query.should.match(clause) }) it('return filename and mimeType != FOLDER when FILE specified', async () => { await driveOps.getFilesInFolder({ withFolderId: 'anyFolderId', ofType: mimeType.FILE, }) logger.debug(fakeDriveService.q) const query = fakeDriveService.q const clause = new RegExp(`anyFolderId.+ mimeType != \\'${mimeType.getType(mimeType.FOLDER)}`) query.should.match(clause) }) it('throw with expected error', async () => { driveOps.init(throwDriveService) let caught = false await driveOps.getFilesInFolder({withFolderId: 'throwFolderId', ofType: undefined}) .catch(err => { err.should.contain('the fake error') // err.response.data.error.errors[0].should.match(/the fake error/) caught = true } ) caught.should.be.true }) }) describe('getFileNamesInFolder() should', () => { it('return array of strings with filenames', async () => { const result = await driveOps.getFileNamesInFolder({withId: 'any', ofType: undefined}) result.should.deep.equal(['fakeName']) }) }) describe('getFilesRecursively() should', () => { it('return the one file of:Type:mimeType.SPREADSHEET', async () => { const result = await driveOps.getFilesRecursively({ withFolderId: 'anyFolderId', ofType: mimeType.SPREADSHEET, }) result.length.should.equal(1) }) it('return no files when no types match ofType:', async () => { const result = await driveOps.getFilesRecursively({ withFolderId: 'anyFolderId', ofType: mimeType.DOC, }) result.length.should.equal(0) }) }) describe('listFiles() should', () => { it('return list of files', async () => { const result = await driveOps.listFiles() result.length.should.equal(1) }) it('throw custom string when files.list throws', async () => { driveOps.init(throwDriveService) let caught = false await driveOps.listFiles() .catch(err => { // test is returning error the same as the google error object err.response.data.error.errors[0].should.match(/the fake error/) caught = true }) caught.should.be.true }) }) })</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="Drive_driveOps.module_test.html">test</a></li><li><a href="module-drive_driveOps.html">drive/driveOps</a></li><li><a href="module-Drive_driveOps%2520Integration%2520Tests.html">Drive/driveOps Integration Tests</a></li><li><a href="module-drive_driveService.html">drive/driveService</a></li><li><a href="module-Sheet_sheetOps%2520Integration%2520Tests.html">Sheet/sheetOps Integration Tests</a></li><li><a href="sheets_sheetOps.module_js.html">sheets/sheetOps.js</a></li><li><a href="sheets_sheetService.module_js.html">sheets/sheetService.js</a></li><li><a href="-_sheetOps.module_test.html">test</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat May 11 2019 23:52:27 GMT-0700 (Pacific Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>