UNPKG

4.66 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const fs = require('fs');
18const JSZip = require('jszip');
19const chai = require('chai');
20const expect = chai.expect;
21
22const FileLoader = require('../lib/fileloader');
23
24describe('FileLoader', () => {
25
26 describe('#loadFileBuffer', () => {
27 it('should return an instance of Buffer', async () => {
28 const content = await FileLoader.loadFileBuffer('./test/data', 'logo.png', true);
29 expect(content).to.be.instanceOf(Buffer);
30 });
31
32 it('should return null if file is not found and required is false', async () => {
33 const content = await FileLoader.loadFileBuffer('./test', 'logo.png', false);
34 expect(content).to.be.null;
35 });
36
37 it('should throw an error if file is not found and required is true', async () => {
38 await expect(FileLoader.loadFileBuffer('./test', 'logo.png', true)).to.be.eventually.rejectedWith(Error);
39 });
40 });
41
42 describe('#loadZipFileBuffer', () => {
43 it('should return an instance of Buffer', () => {
44 const zip = new JSZip();
45 zip.loadAsync(fs.readFileSync('./test/data/logo.zip'))
46 .then(async (zip) => {
47 const content = await FileLoader.loadZipFileBuffer(zip, 'logo.png', true);
48 expect(content).to.be.an.instanceOf(Buffer);
49 });
50 });
51
52 it('should return null if path is not found inside the zip and required is false', async () => {
53 const zip = new JSZip();
54 const content = await FileLoader.loadZipFileBuffer(zip, 'logo.png', false);
55 expect(content).to.be.null;
56 });
57
58 it('should throw an error if path is not found inside the zip and required is true', async () => {
59 const zip = new JSZip();
60 await expect(FileLoader.loadZipFileBuffer(zip, 'logo.png', true)).to.be.eventually.rejectedWith(Error);
61 });
62 });
63
64 describe('#loadZipFileContents', () => {
65 it('should return an instance of file', () => {
66 const zip = new JSZip();
67 zip.loadAsync(fs.readFileSync('../../tests/helloworldcontract.zip'))
68 .then(async (zip) => {
69 expect(
70 await FileLoader.loadZipFileContents(zip, 'helloworldcontract/request.json', true, false)
71 ).to.deep.equal({
72 '$class': 'org.accordproject.helloworld.Request',
73 'input': 'Accord Project',
74 });
75 });
76 });
77
78 it('should return null if path is not found inside the zip and required is false', async () => {
79 const zip = new JSZip();
80 const content = await FileLoader.loadZipFileContents(zip, 'requiest.json', true, false);
81 expect(content).to.be.null;
82 });
83
84 it('should throw an error if path is not found inside the zip and required is true', async () => {
85 const zip = new JSZip();
86 await expect(FileLoader.loadZipFileContents(zip, 'request.json', true, true)).to.be.eventually.rejectedWith(Error);
87 });
88 });
89
90 describe('#loadFileContents', () => {
91 it('should return an instance of file', async () => {
92 const content = await FileLoader.loadFileContents('../../tests/helloworldcontract', 'request.json', true, false);
93 expect(content).to.deep.equal({
94 '$class': 'org.accordproject.helloworld.Request',
95 'input': 'Accord Project',
96 });
97 });
98
99 it('should return null if path is not found and required is false', async () => {
100 const content = await FileLoader.loadFileContents('../../tests/helloworldcontract', 'foo.json', true, false);
101 expect(content).to.be.null;
102 });
103
104 it('should throw an error if path is not found and required is true', async () => {
105 await expect(FileLoader.loadFileContents('../../tests/helloworldcontract', 'foo.json', true, true)).to.be.eventually.rejectedWith(Error);
106 });
107 });
108
109});