Skip to content

Commit 53703f0

Browse files
authored
Merge pull request #23 from mtrezza/increase-test-coverage
Increase test coverage
2 parents b6de4f2 + c670a21 commit 53703f0

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

spec/ApiMailAdapter.spec.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe('ApiMailAdapter', () => {
123123
}
124124
});
125125

126-
it('fails with invalid placeholder callback configuration', async () => {
126+
it('fails with invalid placeholder callback', async () => {
127127
const configs = [
128128
{ apiCallback: df, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds, placeholderCallback: {} } } },
129129
{ apiCallback: df, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds, placeholderCallback: ds } } }
@@ -133,6 +133,29 @@ describe('ApiMailAdapter', () => {
133133
}
134134
});
135135

136+
it('fails with missing or invalid API callback', async () => {
137+
const configs = [
138+
{ sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds } } },
139+
{ apiCallback: null, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds } } },
140+
{ apiCallback: true, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds } } },
141+
{ apiCallback: ds, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds } } },
142+
];
143+
for (const config of configs) {
144+
expect(adapter(config)).toThrow(Errors.Error.apiCallbackNoFunction);
145+
}
146+
});
147+
148+
it('fails with invalid locale callback', async () => {
149+
const configs = [
150+
{ apiCallback: df, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds, localeCallback: ds } } },
151+
{ apiCallback: df, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds, localeCallback: true } } },
152+
{ apiCallback: df, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds, localeCallback: [] } } },
153+
];
154+
for (const config of configs) {
155+
expect(adapter(config)).toThrow(Errors.Error.localeCallbackNoFunction);
156+
}
157+
});
158+
136159
it('succeeds with valid configuration', async () => {
137160
const configs = [
138161
{ apiCallback: df, sender: ds, templates: { customEmail: { subjectPath: ds, textPath: ds } } },
@@ -607,7 +630,7 @@ describe('ApiMailAdapter', () => {
607630
expect(htmlSpyData.toString('utf8')).toEqual(htmlFileData.toString('utf8'));
608631
});
609632

610-
it('falls back to default file if there is no language or locale match', async function () {
633+
it('falls back to default file if there is no language or locale match', async () => {
611634
// Pretend that there are no files in folders `de-AT` and `de`
612635
spyOn(adapter, '_fileExists').and.callFake(async (path) => {
613636
return !/\/templates\/de(-AT)?\//.test(path);
@@ -627,6 +650,16 @@ describe('ApiMailAdapter', () => {
627650
expect(htmlSpyData.toString('utf8')).toEqual(htmlFileData.toString('utf8'));
628651
});
629652

653+
it('falls back to default file if file access throws', async () => {
654+
const getLocalizedFilePathSpy = spyOn(adapter, '_getLocalizedFilePath').and.callThrough();
655+
spyOn(fs, 'access').and.callFake(async () => {
656+
throw 'Test file access error';
657+
});
658+
await adapter._createApiData(options);
659+
const file = await getLocalizedFilePathSpy.calls.all()[0].returnValue;
660+
expect(file).toMatch(options.template.subjectPath);
661+
});
662+
630663
it('makes user locale available in API callback', async () => {
631664
const locale = await options.template.localeCallback();
632665
const email = {

spec/Errors.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const Errors = require('../src/Errors');
4+
5+
describe('Errors', () => {
6+
it('should create a custom error', () => {
7+
const message = "Example Error";
8+
const error = Errors.customError(message);
9+
expect(error.message).toBe(message);
10+
expect(error instanceof Error).toBeTrue();
11+
});
12+
});

src/ApiMailAdapter.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ApiMailAdapter extends MailAdapter {
3030

3131
// Ensure API callback is set
3232
if (typeof apiCallback !== 'function') {
33-
throw Errors.Error.apiClientCallbackNoFuncion;
33+
throw Errors.Error.apiCallbackNoFunction;
3434
}
3535

3636
// Initialize
@@ -311,7 +311,7 @@ class ApiMailAdapter extends MailAdapter {
311311
* @description Loads a file's content.
312312
* @param {String} path The file path.
313313
* @param {String} locale The locale if a localized version of the file should be
314-
* loaded if available, or `undefined` if no localiziation should occur.
314+
* loaded if available, or `undefined` if no localization should occur.
315315
* @returns {Promise<Buffer>} The file content.
316316
*/
317317
async _loadFile(path, locale) {
@@ -393,7 +393,7 @@ class ApiMailAdapter extends MailAdapter {
393393
* @function getLocalizedFilePath
394394
* @description Returns a localized file path matching a given locale.
395395
*
396-
* Localized files are placed in subfolders of the given path, for example:
396+
* Localized files are placed in sub-folders of the given path, for example:
397397
*
398398
* root/
399399
* ├── base/ // base path to files
@@ -414,37 +414,29 @@ class ApiMailAdapter extends MailAdapter {
414414
* if a localized file could not be determined.
415415
*/
416416
async _getLocalizedFilePath(filePath, locale) {
417-
try {
418-
// Get file name and base path
419-
const file = path.basename(filePath);
420-
const basePath = path.dirname(filePath);
421-
422-
// If locale is not set return default file
423-
if (!locale) { return filePath; }
417+
// Get file name and base path
418+
const file = path.basename(filePath);
419+
const basePath = path.dirname(filePath);
424420

425-
// Check file for locale exists
426-
const localePath = path.join(basePath, locale, file);
427-
const localeFileExists = await this._fileExists(localePath);
421+
// If locale is not set return default file
422+
if (!locale) { return filePath; }
428423

429-
// If file for locale exists return file
430-
if (localeFileExists) { return localePath; }
424+
// Check file for locale exists
425+
const localePath = path.join(basePath, locale, file);
426+
const localeFileExists = await this._fileExists(localePath);
431427

432-
// Check file for language exists
433-
const languagePath = path.join(basePath, locale.split("-")[0], file);
434-
const languageFileExists = await this._fileExists(languagePath);
428+
// If file for locale exists return file
429+
if (localeFileExists) { return localePath; }
435430

436-
// If file for language exists return file
437-
if (languageFileExists) { return languagePath; }
431+
// Check file for language exists
432+
const languagePath = path.join(basePath, locale.split("-")[0], file);
433+
const languageFileExists = await this._fileExists(languagePath);
438434

439-
// Return default file path
440-
return filePath;
435+
// If file for language exists return file
436+
if (languageFileExists) { return languagePath; }
441437

442-
} catch (e) {
443-
console.log("error in getLocalizedFilePath: " + e);
444-
445-
// Return default file path
446-
return filePath;
447-
}
438+
// Return default file path
439+
return filePath;
448440
}
449441

450442
/**

src/Errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Errors extends Error {
1616
configurationInvalid: new Error('ApiMailAdapter: configuration is missing or invalid.'),
1717
templatesInvalid: new Error('ApiMailAdapter: templates are missing or invalid.'),
1818
templateContentPathInvalid: new Error('ApiMailAdapter: template content path is invalid.'),
19-
apiCallbackNoFuncion: new Error('ApiMailAdapter: API callback is not a function.'),
19+
apiCallbackNoFunction: new Error('ApiMailAdapter: API callback is not a function.'),
2020
templateCallbackNoFunction: new Error('ApiMailAdapter: placeholder callback is not a function.'),
2121
localeCallbackNoFunction: new Error('ApiMailAdapter: locale callback is not a function.'),
2222
templateConfigurationNoName: new Error('ApiMailAdapter: template name is missing.'),

0 commit comments

Comments
 (0)