Skip to content

Instantly share code, notes, and snippets.

@snoblenet
Last active September 19, 2018 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoblenet/54edd228681d34559f63ecf5eeb9dd44 to your computer and use it in GitHub Desktop.
Save snoblenet/54edd228681d34559f63ecf5eeb9dd44 to your computer and use it in GitHub Desktop.
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