Skip to content

Commit bf655ce

Browse files
authored
Jest snapshots: classes in animations should autoexpand. (#4647)
* Updates.. * Updating serialization.
1 parent 57d14eb commit bf655ce

File tree

7 files changed

+86
-7
lines changed

7 files changed

+86
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@uifabric/jest-serializer-merge-styles",
5+
"comment": "Updating how keyframe classes are serialized in results.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@uifabric/jest-serializer-merge-styles",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@uifabric/merge-styles",
5+
"comment": "Updating how keyframe classes are cached to aid with jest snapshot testing.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@uifabric/merge-styles",
10+
"email": "[email protected]"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "office-ui-fabric-react",
5+
"comment": "Updating a snapshot test.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "office-ui-fabric-react",
10+
"email": "[email protected]"
11+
}

packages/jest-serializer-merge-styles/src/index.test.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { print, test } from './index';
2-
import { Stylesheet, InjectionMode, mergeStyles } from '@uifabric/merge-styles';
2+
import { Stylesheet, InjectionMode, mergeStyles, keyframes } from '@uifabric/merge-styles';
3+
4+
const indent = (val: string): string => ' ' + val;
35

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

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

27-
const indent = (val: string): string => ' ' + val;
2829
const classNames = mergeStyles(
2930
'ms-GlobalClassName',
3031
{
@@ -57,4 +58,29 @@ describe('print', () => {
5758
].join('\n'));
5859
});
5960

61+
it('can expand animation class names', () => {
62+
const fadeInClassName = keyframes({
63+
from: { opacity: 0 },
64+
to: { opacity: 1 }
65+
});
66+
67+
const className = mergeStyles({
68+
animationName: fadeInClassName
69+
});
70+
71+
expect(
72+
print(
73+
className,
74+
() => '',
75+
indent
76+
)
77+
).toEqual([
78+
'',
79+
'',
80+
indent('{'),
81+
indent(' animation-name: keyframes from{opacity:0;}to{opacity:1;};'),
82+
indent('}'),
83+
].join('\n'));
84+
});
85+
6086
});

packages/jest-serializer-merge-styles/src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function test(val: string): boolean {
4040

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

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

5152
insertedRules.split(';').sort().forEach((rule: string) => {
5253
if (rule) {
53-
ruleStrings.push(indent(' ' + rule.replace(':', ': ') + ';'));
54+
const [name, value] = rule.split(':');
55+
const valueParts = value.split(' ');
56+
let result: string[] = [];
57+
58+
for (const part of valueParts) {
59+
const ruleSet = stylesheet.insertedRulesFromClassName(part);
60+
61+
if (ruleSet) {
62+
result = result.concat(ruleSet);
63+
} else {
64+
result.push(part);
65+
}
66+
}
67+
68+
ruleStrings.push(indent(` ${name}: ${result.join(' ')};`));
5469
}
5570
});
5671

packages/merge-styles/src/keyframes.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export function keyframes(timeline: { [key: string]: {} }): string {
2121

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

24-
// If needed later, we would add vendor prefixes here.
24+
stylesheet.cacheClassName(
25+
name,
26+
rules,
27+
[],
28+
['keyframes', rules]
29+
);
2530

2631
return name;
2732
}

packages/office-ui-fabric-react/src/components/ActivityItem/__snapshots__/ActivityItem.test.tsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ exports[`ActivityItem renders compact with animation correctly 1`] = `
302302
align-items: center;
303303
animation-duration: .5s;
304304
animation-iteration-count: 1;
305-
animation-name: css-93;
305+
animation-name: keyframes from{opacity:0;}to{opacity:1;};
306306
box-sizing: border-box;
307307
color: #666666;
308308
display: flex;
@@ -351,7 +351,7 @@ exports[`ActivityItem renders compact with animation correctly 1`] = `
351351
{
352352
animation-duration: .8s;
353353
animation-iteration-count: 1;
354-
animation-name: css-92;
354+
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%{};
355355
border-radius: 225px;
356356
border-style: solid;
357357
height: 0px;
@@ -493,7 +493,7 @@ exports[`ActivityItem renders compact with animation correctly 1`] = `
493493
{
494494
animation-duration: .5s;
495495
animation-iteration-count: 1;
496-
animation-name: css-94;
496+
animation-name: keyframes from{transform:translateX(-10px);}to{transform:translateX(0);};
497497
flex: 1;
498498
overflow-x: hidden;
499499
padding-bottom: 0;

0 commit comments

Comments
 (0)