Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The contract between getPrefix() and prefixWord() is enforced by the specs
// utils/get_prefix.js
export const getPrefix = (lang) => {
if (lang === 'fr') return 'méga';
return 'mega';
};
// utils/prefix_word.js
export const prefixWord = (prefixGetter, wordToPrefix) => prefixGetter() + wordToPrefix;
// spec/fixtures/prefix_fixtures.js
export const prefixFixtures = {
en: { args: 'en', result: 'mega' },
fr: { args: 'en', result: 'méga' },
};
// spec/utils/get_prefix_spec.js
import getPrefix from '../../utils/get_prefix';
import prefixFixtures from '../fixtures/prefix_fixtures';
const { args, result } = prefixFixtures.en;
describe('getPrefix()', => {
it('should return mega', () =>
expect(getPrefix(...args)).to.equal(result));
});
// spec/utils/prefix_word_spec.js
import sinon from 'sinon';
import prefixWord from '../../utils/prefix_word';
import prefixFixtures from '../../utils/prefix_fixtures';
const { args, result } = prefixFixtures.en;
const getPrefix = sinon.stub().returns(result);
describe('prefixWord()', => {
it('should call getPrefix with the correct args', () =>
expect(prefixWord.args).to.deep.equal([args]));
it('should prefix the supplied word', =>
expect(prefixWord(getPrefix, 'Word')).to.equal('megaWord'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.