Skip to content
Permalink
Browse files

[637] Unit tests for generateFileNameWithTimestamp

  • Loading branch information...
daniloprates committed May 16, 2019
1 parent f6fcaed commit 7daf3fe03ccfbdb7a5ada7b983580e64d1348767
@@ -0,0 +1,21 @@
@unit
Feature: Generate fileName with timestamp

Scenario: I don't pass any props and use the default ones
Given I dont pass any props to the function
Then the prefix should be "logs"
And the extention should be "zip"
And the time should be converted into UTC

Scenario Outline: I pass the following props
Given I pass the following props to the function:
| prefix | extention | isUTC |
| <prefix> | <extention> | <isUTC> |
Then the prefix should be "<prefix>"
And the extention should be "<extention>"
And the time <isUTC> be converted into UTC

Examples:
| prefix | extention | isUTC |
| testPrefix | docx | should |
| | pdf | shouldn't |
@@ -0,0 +1,57 @@
import { Given, When, Then } from 'cucumber';
import { expect } from 'chai';
import { pickBy, identity } from 'lodash';
import {
generateFileNameWithTimestamp,
defaultProps,
} from '../../../../source/common/utils/files';

const getDataFromFunction = props => {
const filename = generateFileNameWithTimestamp(props);
const prefix = filename.match(/^[^-]*[^ -]/i)[0];
const extention = filename.match(/\.[0-9a-z]+$/i)[0].replace('.', '');
const isUTC = !!filename.match(`Z.${extention}`);
return {
filename,
prefix,
extention,
isUTC,
};
};

Given('I dont pass any props to the function', function() {
const { filename, extention, prefix, isUTC } = getDataFromFunction();
this.context.filename = filename;
this.context.extention = extention;
this.context.prefix = prefix;
this.context.isUTC = isUTC;
});

Given('I pass the following props to the function:', function(data) {
let [expectedProps] = data.hashes();
expectedProps = pickBy(expectedProps, identity);
if (expectedProps.isUTC)
expectedProps.isUTC = Boolean(expectedProps.isUTC === 'should');
const { filename, extention, prefix, isUTC } = getDataFromFunction(
expectedProps
);
this.context.filename = filename;
this.context.extention = extention;
this.context.prefix = prefix;
this.context.isUTC = isUTC;
});

Then('the prefix should be {string}', function(prefix) {
const expectedPrefix = prefix || defaultProps.prefix;
expect(this.context.prefix).to.equal(expectedPrefix);
});

Then('the extention should be {string}', function(extention) {
const expectedExtention = extention || defaultProps.extention;
expect(this.context.extention).to.equal(expectedExtention);
});

Then(/^the time (should|shouldn't) be converted into UTC/, function(state) {
const isUTC = state === 'should';
expect(isUTC).to.equal(this.context.isUTC);
});
@@ -1,22 +1,22 @@
// @flow
import moment from 'moment';

const defaultProps = {
export const defaultProps = {
prefix: 'logs',
fileType: 'zip',
extention: 'zip',
date: moment(),
isUTC: true,
};

type Props = {
prefix?: string,
fileType?: string,
extention?: string,
date?: moment,
isUTC?: boolean,
};

export const generateFileNameWithTimestamp = (props?: Props) => {
const { prefix, fileType, isUTC } = {
const { prefix, extention, isUTC } = {
...defaultProps,
...props,
};
@@ -31,17 +31,17 @@ export const generateFileNameWithTimestamp = (props?: Props) => {
z = 'Z';
}
return `${prefix}-${`${date.format('YYYY-MM-DDTHHmmss.0SSS')}${z}`}${
fileType ? '.' : ''
}${fileType}`;
extention ? '.' : ''
}${extention}`;
};

export const isFileNameWithTimestamp = (
prefix: string = 'logs',
fileType: string = 'zip'
extention: string = 'zip'
) => (fileName: string) =>
fileName.match(
RegExp(
`(${prefix}-)([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{6}.0[0-9]{3}Z)(.${fileType})`
`(${prefix}-)([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{6}.0[0-9]{3}Z)(.${extention})`
)
);

@@ -47,7 +47,7 @@ export default class InstructionsDialogContainer extends Component<Props> {
const name = generateFileNameWithTimestamp({
prefix: 'paper-wallet-certificate',
date,
fileType: '',
extention: '',
isUTC: false,
});

0 comments on commit 7daf3fe

Please sign in to comment.
You can’t perform that action at this time.