Skip to content

Commit

Permalink
added support for adding a customSeparator (meinaart#82)
Browse files Browse the repository at this point in the history
* added support for adding a customSeparator

* fix tests

* updated readme
  • Loading branch information
erwinheitzman authored Feb 20, 2020
1 parent 93f7e61 commit 7379455
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ it('toMatchImageSnapshot - whole page', () => {
You can pass the following options to `toMatchImageSnapshot` to override default behavior.
```javascript
{
"createDiffImage": true, // Should a "diff image" be created, can be disabled for performance
"threshold": 0.01, // Amount in pixels or percentage before snapshot image is invalid
"name": "custom image name", // Naming resulting image file with a custom name rather than concatenating test titles
"thresholdType": "percent", // Can be either "pixel" or "percent"
"createDiffImage": true, // Should a "diff image" be created, can be disabled for performance
"threshold": 0.01, // Amount in pixels or percentage before snapshot image is invalid
"name": "custom image name", // Naming resulting image file with a custom name rather than concatenating test titles
"separator": "custom image separator", // Naming resulting image file with a custom separator rather than using the default ` #`
"thresholdType": "percent", // Can be either "pixel" or "percent"
}
```

Expand Down
4 changes: 3 additions & 1 deletion src/commands/toMatchImageSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const logMessage = require('../utils/commands/logMessage');
const { NO_LOG } = require('../constants');
const { COMMAND_MATCH_IMAGE_SNAPSHOT: commandName } = require('./commandNames');
const getImageData = require('../utils/image/getImageData');
const { getImageConfig, getScreenshotConfig, getCustomName } = require('../config');
const { getImageConfig, getScreenshotConfig, getCustomName, getCustomSeparator } = require('../config');

function afterScreenshot(taskData) {
return ($el, props) => {
Expand All @@ -22,11 +22,13 @@ function afterScreenshot(taskData) {
async function toMatchImageSnapshot(subject, commandOptions) {
const options = getImageConfig(commandOptions);
const customName = getCustomName(commandOptions);
const customSeparator = getCustomSeparator(commandOptions);

const taskData = await getTaskData({
commandName,
options,
customName,
customSeparator,
subject,
});

Expand Down
6 changes: 6 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ function getCustomName(suppliedConfig) {
return cfg.name;
}

function getCustomSeparator(suppliedConfig) {
const cfg = suppliedConfig || getConfig();
return cfg.separator;
}

function getServerUrl(suppliedConfig) {
const cfg = suppliedConfig || getConfig();
return `http://${cfg.serverHost}:${cfg.serverPort}/?token=${cfg.token}`;
Expand All @@ -126,6 +131,7 @@ module.exports = {
getPrettierConfig,
getScreenshotConfig,
getCustomName,
getCustomSeparator,
getServerUrl,
initConfig,
shouldNormalize,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/commands/getTaskData.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ async function getTaskData({
commandName,
options,
customName,
customSeparator,
subject: testSubject
} = {}) {
const subjectIsImage = isImage(commandName);
const test = getTestForTask();
const testTitle = getTestTitle(test);
const spec = await getSpec();
const testFile = spec.absolute;
const snapshotTitle = getSnapshotTitle(test, customName, subjectIsImage);
const snapshotTitle = getSnapshotTitle(test, customName, customSeparator, subjectIsImage);
const subject = subjectIsImage ? testSubject : getSubject(testSubject);
const dataType = getDataType({commandName, subject: testSubject});

Expand Down
5 changes: 3 additions & 2 deletions src/utils/snapshotTitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ function snapshotTitleIsUsed(snapshotTitle, isImage = false) {
return (isImage ? SNAPSHOT_TITLES_IMAGE : SNAPSHOT_TITLES_TEXT).indexOf(snapshotTitle) !== -1;
}

function getSnapshotTitle(test, customName, isImage = false) {
function getSnapshotTitle(test, customName, customSeparator, isImage = false) {
const name = customName || getTestTitle(test);
const separator = customSeparator || ' #';
const snapshots = isImage ? SNAPSHOTS_IMAGE : SNAPSHOTS_TEXT;

if (snapshots[name] !== undefined) {
Expand All @@ -20,7 +21,7 @@ function getSnapshotTitle(test, customName, isImage = false) {
snapshots[name] = 0;
}

const snapshotTitle = `${name} #${snapshots[name]}`;
const snapshotTitle = `${name}${separator}${snapshots[name]}`;
(isImage ? SNAPSHOT_TITLES_IMAGE : SNAPSHOT_TITLES_TEXT).push(snapshotTitle);
return snapshotTitle;
}
Expand Down

0 comments on commit 7379455

Please sign in to comment.