|
| 1 | +const azure = jest.genMockFromModule('azure-storage'); |
| 2 | +const stream = require('stream'); |
| 3 | + |
| 4 | +jest.mock('fs-extra', () => { |
| 5 | + return { |
| 6 | + createReadStream: jest.fn(() => 'this is a stream'), |
| 7 | + readFile: jest.fn(cb => cb(null, 'file content!')) |
| 8 | + }; |
| 9 | +}); |
| 10 | + |
| 11 | +jest.mock('node-dir', () => { |
| 12 | + return { |
| 13 | + paths: jest.fn((pathToDir, cb) => { |
| 14 | + cb(null, { |
| 15 | + files: [ |
| 16 | + `${pathToDir}\\package.json`, |
| 17 | + `${pathToDir}\\server.js`, |
| 18 | + `${pathToDir}\\.env`, |
| 19 | + `${pathToDir}\\template.js`, |
| 20 | + `${pathToDir}/package.json`, |
| 21 | + `${pathToDir}/server.js`, |
| 22 | + `${pathToDir}/.env`, |
| 23 | + `${pathToDir}/template.js` |
| 24 | + ] |
| 25 | + }); |
| 26 | + }) |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +let cachedTxt = 0; |
| 31 | +let cachedJson = 0; |
| 32 | + |
| 33 | +const blobServiceClient = { |
| 34 | + getContainerClient(containerName) { |
| 35 | + return { |
| 36 | + getBlobClient(filePath) { |
| 37 | + return { |
| 38 | + async download() { |
| 39 | + cachedTxt++; |
| 40 | + cachedJson++; |
| 41 | + let notExistsError = { |
| 42 | + name: 'StorageError', |
| 43 | + code: 'BlobNotFound', |
| 44 | + statusCode: 404 |
| 45 | + }; |
| 46 | + const contents = { |
| 47 | + 'path/test.txt': { content: 'Hello!' }, |
| 48 | + 'path/test.json': { content: JSON.stringify({ data: 'Hello!' }) }, |
| 49 | + 'path/not-found.txt': { error: notExistsError }, |
| 50 | + 'path/not-found.json': { error: notExistsError }, |
| 51 | + 'path/not-a-json.json': { content: 'Not a json' }, |
| 52 | + 'path/to-mutable.json': { |
| 53 | + content: JSON.stringify({ value: cachedJson }) |
| 54 | + }, |
| 55 | + 'path/to-mutable.txt': { content: String(cachedTxt) } |
| 56 | + }; |
| 57 | + |
| 58 | + const testResult = contents[filePath]; |
| 59 | + |
| 60 | + if (testResult.error) throw testResult.error; |
| 61 | + |
| 62 | + return { |
| 63 | + readableStreamBody: stream.Readable.from([testResult.content]) |
| 64 | + }; |
| 65 | + } |
| 66 | + }; |
| 67 | + }, |
| 68 | + getBlockBlobClient(fileName) { |
| 69 | + return { |
| 70 | + async uploadData(content, settings) { |
| 71 | + if (fileName.indexOf('error') >= 0) { |
| 72 | + throw { msg: 'sorry' }; |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + fileName, |
| 77 | + lengthWritten: content.length, |
| 78 | + container: containerName, |
| 79 | + settings |
| 80 | + }; |
| 81 | + } |
| 82 | + }; |
| 83 | + }, |
| 84 | + listBlobsByHierarchy(delimiter, { prefix }) { |
| 85 | + return { |
| 86 | + async *[Symbol.asyncIterator]() { |
| 87 | + const entries = { |
| 88 | + 'components/': [ |
| 89 | + { name: 'components/image', kind: 'prefix' }, |
| 90 | + { |
| 91 | + name: 'components/components/details.json', |
| 92 | + kind: 'blob' |
| 93 | + } |
| 94 | + ], |
| 95 | + 'components/image/': [ |
| 96 | + { name: 'components/image/1.0.0', kind: 'prefix' }, |
| 97 | + { name: 'components/image/1.0.1', kind: 'prefix' } |
| 98 | + ], |
| 99 | + 'components/image/1.0.0': [ |
| 100 | + { name: 'components/image/1.0.1/image.png', kind: 'blob' } |
| 101 | + ] |
| 102 | + }; |
| 103 | + |
| 104 | + const blobs = entries[prefix] || []; |
| 105 | + |
| 106 | + for (const entry of blobs) { |
| 107 | + yield entry; |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +azure.StorageSharedKeyCredential = jest.fn(function (account, key) { |
| 117 | + return { account, key }; |
| 118 | +}); |
| 119 | +azure.BlobServiceClient = jest.fn(function (url, credentials) { |
| 120 | + return { _credentials: credentials, _url: url, ...blobServiceClient }; |
| 121 | +}); |
| 122 | +module.exports = azure; |
0 commit comments