From fcce26d4e1a4116f702dcecaa8be746311abfb29 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 21 Mar 2024 08:36:05 +0200 Subject: [PATCH] feature: cloudfunc: get rid of mock-require --- .../common/entity.js => common/entity.spec.js | 8 +- test/common/cloudfunc.js | 83 +++++++++---------- 2 files changed, 42 insertions(+), 49 deletions(-) rename test/common/entity.js => common/entity.spec.js (91%) diff --git a/test/common/entity.js b/common/entity.spec.js similarity index 91% rename from test/common/entity.js rename to common/entity.spec.js index 168318e8..59fe68f0 100644 --- a/test/common/entity.js +++ b/common/entity.spec.js @@ -1,12 +1,12 @@ 'use strict'; const test = require('supertape'); -const entity = require('../../common/entity'); +const entity = require('./entity'); test('cloudcmd: entity: encode', (t) => { const result = entity.encode(' '); const expected = '<hello> '; - + t.equal(result, expected, 'should encode entity'); t.end(); }); @@ -14,7 +14,7 @@ test('cloudcmd: entity: encode', (t) => { test('cloudcmd: entity: decode', (t) => { const result = entity.decode('<hello> '); const expected = ' '; - + t.equal(result, expected, 'should decode entity'); t.end(); }); @@ -22,7 +22,7 @@ test('cloudcmd: entity: decode', (t) => { test('cloudcmd: entity: encode quote', (t) => { const result = entity.encode('"hello"'); const expected = '"hello"'; - + t.equal(result, expected, 'should encode entity'); t.end(); }); diff --git a/test/common/cloudfunc.js b/test/common/cloudfunc.js index ee942b59..ae5b0063 100644 --- a/test/common/cloudfunc.js +++ b/test/common/cloudfunc.js @@ -1,23 +1,17 @@ 'use strict'; const fs = require('node:fs'); + const tryCatch = require('try-catch'); +const test = require('supertape'); +const htmlLooksLike = require('html-looks-like'); +const readFilesSync = require('@cloudcmd/read-files-sync'); + +const {time, timeEnd} = require(`../../common/util`); +const CloudFunc = require('../../common/cloudfunc.js'); const DIR = `${__dirname}/../../`; -const COMMONDIR = `${DIR}common/`; - -const {time, timeEnd} = require(`${COMMONDIR}util`); - -const CloudFuncPath = `${COMMONDIR}cloudfunc`; - -const CloudFunc = require(CloudFuncPath); - -const test = require('supertape'); - -const {reRequire} = require('mock-require'); -const htmlLooksLike = require('html-looks-like'); - -const readFilesSync = require('@cloudcmd/read-files-sync'); +const CloudFuncPath = `cloudfunc`; const TMPLDIR = `${DIR}tmpl/`; const FS_DIR = `${TMPLDIR}fs/`; @@ -66,40 +60,40 @@ let Expect = '
' + test('cloudfunc: render', (t) => { const template = readFilesSync(FS_DIR, TMPL, 'utf8'); - + time('CloudFunc.buildFromJSON'); const result = CloudFunc.buildFromJSON({ prefix: '', data, template, }); - + Expect += fs.readFileSync(EXPECT_PATH, 'utf8'); - + let i; const isNotOk = Expect .split('') .some((item, number) => { const ret = result[number] !== item; - + if (ret) i = number; - + return ret; }); - + timeEnd('CloudFunc.buildFromJSON'); - + if (isNotOk) { console.log(`Error in char number: ${i}\n`, `Expect: ${Expect.substr(i)}\n`, `Result: ${result.substr(i)}`); - + console.log('buildFromJSON: Not OK'); } - + t.equal(result, Expect, 'should be equal rendered json data'); - + htmlLooksLike(result, Expect); - + t.end(); }); @@ -107,9 +101,9 @@ test('cloudfunc: formatMsg', (t) => { const msg = 'hello'; const name = 'name'; const status = 'ok'; - + const result = CloudFunc.formatMsg(msg, name, status); - + t.equal(result, 'hello: ok("name")'); t.end(); }); @@ -118,44 +112,42 @@ test('cloudfunc: formatMsg: no name', (t) => { const msg = 'hello'; const name = null; const status = 'ok'; - + const result = CloudFunc.formatMsg(msg, name, status); - + t.equal(result, 'hello: ok'); t.end(); }); test('cloudfunc: getTitle', (t) => { - const CloudFunc = reRequire(CloudFuncPath); - - const result = CloudFunc.getTitle(); - + const result = CloudFunc.getTitle({ + path: '/', + }); + t.equal(result, 'Cloud Commander - /'); t.end(); }); test('cloudfunc: getTitle: no name', (t) => { - const CloudFunc = reRequire(CloudFuncPath); const path = '/hello/world'; - + const result = CloudFunc.getTitle({ path, }); - + t.equal(result, 'Cloud Commander - /hello/world'); t.end(); }); test('cloudfunc: getTitle: name, path', (t) => { - const CloudFunc = reRequire(CloudFuncPath); const name = 'hello'; const path = '/hello/world'; - + const result = CloudFunc.getTitle({ name, path, }); - + t.equal(result, 'hello - /hello/world'); t.end(); }); @@ -164,17 +156,17 @@ test('cloudfunc: getHeaderField', (t) => { const sort = 'name'; const order = 'desc'; const name = 'name'; - + const result = CloudFunc.getHeaderField(sort, order, name); const expected = 'name↓'; - + t.equal(result, expected, 'should set desc arrow'); t.end(); }); test('cloudfunc: getPathLink: no url', (t) => { const [error] = tryCatch(CloudFunc.getPathLink); - + t.ok(error, 'should throw when no url'); t.end(); }); @@ -183,21 +175,22 @@ test('cloudfunc: getPathLink: no template', (t) => { const url = 'http://abc.com'; const prefix = ''; const [error] = tryCatch(CloudFunc.getPathLink, url, prefix); - + t.ok(error, 'should throw when no template'); t.end(); }); test('cloudfunc: getDotDot', (t) => { const dotDot = CloudFunc.getDotDot('/home'); - + t.equal(dotDot, '/', 'should return root'); t.end(); }); test('cloudfunc: getDotDot: two levels deep', (t) => { const dotDot = CloudFunc.getDotDot('/home/coderaiser/'); - + t.equal(dotDot, '/home', 'should return up level'); t.end(); }); +