Hey there, I'm currently working on a unit test for a function. While the technical details are not crucial, I thought it would be helpful to provide some code snippets for better understanding.
Check out the function below:
export function getSalutations(lang: Salutation = Salutation.default): string[] {
return SALUTATIONS[lang] || SALUTATIONS.default;
};
export const enum Salutation {
de,
es,
default
};
const SALUTATIONS = {
de: ['Hr.', 'Fr.'],
es: ['Sr.', 'Sra.'],
default: ['Mr.', 'Mrs.', 'Ms.']
};
Here's an example of a unit test scenario:
QUnit.module('getSalutations', (hooks) => {
QUnit.test('null / default salutations', (assert) => {
assert.ok(DW.Utils.isEqual(DW.Utils.getSalutations(null), ['Mr.', 'Mrs.', 'Ms.']));
});
});
I have encountered a dilemma regarding how to approach testing without duplicating code. Ideally, I'd like to reuse the SALUTATIONS
object for my tests to avoid redundancy. However, since it is not exported from its current scope, I am unable to access it.
At this point, I see two possible solutions:
- Replicate code as mocked data for testing purposes.
- Expose encapsulated code solely for unit testing.
Neither option seems ideal to me, so I am reaching out in the hopes that someone can suggest a more optimal solution. Perhaps there are alternative approaches that I haven't considered yet?
Appreciate any insights!