Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest snapshots: classes in animations should autoexpand. #4647

Merged
merged 2 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@uifabric/jest-serializer-merge-styles",
"comment": "Updating how keyframe classes are serialized in results.",
"type": "minor"
}
],
"packageName": "@uifabric/jest-serializer-merge-styles",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@uifabric/merge-styles",
"comment": "Updating how keyframe classes are cached to aid with jest snapshot testing.",
"type": "minor"
}
],
"packageName": "@uifabric/merge-styles",
"email": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "office-ui-fabric-react",
"comment": "Updating a snapshot test.",
"type": "patch"
}
],
"packageName": "office-ui-fabric-react",
"email": "[email protected]"
}
30 changes: 28 additions & 2 deletions packages/jest-serializer-merge-styles/src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { print, test } from './index';
import { Stylesheet, InjectionMode, mergeStyles } from '@uifabric/merge-styles';
import { Stylesheet, InjectionMode, mergeStyles, keyframes } from '@uifabric/merge-styles';

const indent = (val: string): string => ' ' + val;

const _stylesheet: Stylesheet = Stylesheet.getInstance();
_stylesheet.setConfig({ injectionMode: InjectionMode.none });
Expand All @@ -24,7 +26,6 @@ describe('print', () => {

it('can format, sort, and indent the class names', () => {

const indent = (val: string): string => ' ' + val;
const classNames = mergeStyles(
'ms-GlobalClassName',
{
Expand Down Expand Up @@ -57,4 +58,29 @@ describe('print', () => {
].join('\n'));
});

it('can expand animation class names', () => {
const fadeInClassName = keyframes({
from: { opacity: 0 },
to: { opacity: 1 }
});

const className = mergeStyles({
animationName: fadeInClassName
});

expect(
print(
className,
() => '',
indent
)
).toEqual([
'',
'',
indent('{'),
indent(' animation-name: keyframes from{opacity:0;}to{opacity:1;};'),
indent('}'),
].join('\n'));
});

});
17 changes: 16 additions & 1 deletion packages/jest-serializer-merge-styles/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function test(val: string): boolean {

function _serializeRules(rules: string[], indent: (val: string) => string): string {
const ruleStrings: string[] = [];
const stylesheet = Stylesheet.getInstance();

for (let i = 0; i < rules.length; i += 2) {
const selector = rules[i];
Expand All @@ -50,7 +51,21 @@ function _serializeRules(rules: string[], indent: (val: string) => string): stri

insertedRules.split(';').sort().forEach((rule: string) => {
if (rule) {
ruleStrings.push(indent(' ' + rule.replace(':', ': ') + ';'));
const [name, value] = rule.split(':');
const valueParts = value.split(' ');
let result: string[] = [];

for (const part of valueParts) {
const ruleSet = stylesheet.insertedRulesFromClassName(part);

if (ruleSet) {
result = result.concat(ruleSet);
} else {
result.push(part);
}
}

ruleStrings.push(indent(` ${name}: ${result.join(' ')};`));
}
});

Expand Down
7 changes: 6 additions & 1 deletion packages/merge-styles/src/keyframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export function keyframes(timeline: { [key: string]: {} }): string {

stylesheet.insertRule(`@keyframes ${name}{${rules}}`);

// If needed later, we would add vendor prefixes here.
stylesheet.cacheClassName(
name,
rules,
[],
['keyframes', rules]
);

return name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ exports[`ActivityItem renders compact with animation correctly 1`] = `
align-items: center;
animation-duration: .5s;
animation-iteration-count: 1;
animation-name: css-93;
animation-name: keyframes from{opacity:0;}to{opacity:1;};
box-sizing: border-box;
color: #666666;
display: flex;
Expand Down Expand Up @@ -351,7 +351,7 @@ exports[`ActivityItem renders compact with animation correctly 1`] = `
{
animation-duration: .8s;
animation-iteration-count: 1;
animation-name: css-92;
animation-name: keyframes 0%{border-color:#0078d4;border-width:0px;width:4px;height:4px;}14.2%{opacity:1;border-width:4px;}35.7%{opacity:1;}71.4%{border-width:0;width:28px;height:28px;opacity:0;border-color:#71afe5;}100%{};
border-radius: 225px;
border-style: solid;
height: 0px;
Expand Down Expand Up @@ -493,7 +493,7 @@ exports[`ActivityItem renders compact with animation correctly 1`] = `
{
animation-duration: .5s;
animation-iteration-count: 1;
animation-name: css-94;
animation-name: keyframes from{transform:translateX(-10px);}to{transform:translateX(0);};
flex: 1;
overflow-x: hidden;
padding-bottom: 0;
Expand Down