leumas-universal-preset
Version:
A microservice to declare universal functions packed with reusable props via preset mappings.
46 lines (39 loc) • 2.13 kB
JavaScript
const { expect } = require('chai');
const { withPreset, mappings } = require('../index');
// Define a simple getWeather function that uses positional parameters.
// It returns a dummy temperature based on the unit.
const getWeather = (city, unit = 'Celsius') => {
console.log("City from preset : ", city)
const temperature = unit === 'Fahrenheit' ? '72°F' : '22°C';
return { city, temperature };
};
describe('getWeather withPreset Tests', () => {
// Retrieve the default mapping for getWeather.
const getWeatherMapping = mappings.getWeather.default; // { city: 0, unit: 1 }
it('should return weather for London using preset', () => {
const londonPreset = { city: 'London', unit: 'Celsius' };
const getWeatherLondon = withPreset(getWeather, londonPreset, { mapping: getWeatherMapping });
const result = getWeatherLondon();
expect(result).to.deep.equal({ city: 'London', temperature: '22°C' });
});
it('should return weather for New York using preset', () => {
const newYorkPreset = { city: 'New York', unit: 'Fahrenheit' };
const getWeatherNY = withPreset(getWeather, newYorkPreset, { mapping: getWeatherMapping });
const result = getWeatherNY();
expect(result).to.deep.equal({ city: 'New York', temperature: '72°F' });
});
it('should allow override of preset city', () => {
const tokyoPreset = { city: 'Tokyo', unit: 'Celsius' };
const getWeatherTokyo = withPreset(getWeather, tokyoPreset, { mapping: getWeatherMapping });
// Override the preset city by providing a different value.
const result = getWeatherTokyo('Osaka');
expect(result).to.deep.equal({ city: 'Osaka', temperature: '22°C' });
});
it('should allow override of preset unit', () => {
const parisPreset = { city: 'Paris', unit: 'Celsius' };
const getWeatherParis = withPreset(getWeather, parisPreset, { mapping: getWeatherMapping });
// Override the unit argument to Fahrenheit.
const result = getWeatherParis(undefined, 'Fahrenheit');
expect(result).to.deep.equal({ city: 'Paris', temperature: '72°F' });
});
});