-
Notifications
You must be signed in to change notification settings - Fork 42
/
shared-tracking.js
145 lines (123 loc) · 3.91 KB
/
shared-tracking.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React, { Component } from "react";
import PropTypes from "prop-types";
import { MockEdition } from "@times-components/fixture-generator";
import Link from "@times-components/link";
import { SectionContext } from "@times-components/context";
import StarButton from "@times-components/star-button";
import TestRenderer from "react-test-renderer";
import Section from "../src/section";
jest.mock("@times-components/icons", () => ({
IconForwardArrow: "IconForwardArrow",
IconStar: "IconStar",
IconVideo: "IconVideo"
}));
jest.mock("react-native", () => {
const rn = require.requireActual("react-native");
rn.NativeModules.SectionEvents = {
getSavedArticles: jest.fn().mockReturnValue(Promise.resolve(true))
};
return rn;
});
jest.mock("@times-components/image", () => ({
__esModule: true,
default: "TimesImage"
}));
class WithTrackingContext extends Component {
getChildContext() {
const { stream } = this.props;
return {
tracking: {
analytics: stream
}
};
}
render() {
const { onArticlePress, onPuzzlePress, section } = this.props;
return (
<Section
analyticsStream={() => {}}
onArticlePress={onArticlePress}
onPuzzlePress={onPuzzlePress}
publicationName="TIMES"
section={section}
/>
);
}
}
WithTrackingContext.childContextTypes = {
tracking: PropTypes.shape({
analytics: PropTypes.func
})
};
WithTrackingContext.propTypes = {
onArticlePress: PropTypes.func.isRequired,
onPuzzlePress: PropTypes.func.isRequired,
section: PropTypes.shape({}).isRequired,
stream: PropTypes.func.isRequired
};
export default () => {
it("default section page click tracking", () => {
const edition = new MockEdition().get();
const stream = jest.fn();
const onArticlePress = jest.fn();
const testInstance = TestRenderer.create(
<WithTrackingContext
onArticlePress={onArticlePress}
onPuzzlePress={() => {}}
section={edition.sections[0]}
stream={stream}
/>
);
const [link] = testInstance.root.findAllByType(Link);
link.props.onPress();
const [[call]] = stream.mock.calls;
expect(call).toMatchSnapshot();
expect(onArticlePress.mock.calls).toMatchSnapshot("onArticlePress");
});
it("puzzle section page click tracking", () => {
const edition = new MockEdition().get();
const stream = jest.fn();
const onPuzzlePress = jest.fn();
const testInstance = TestRenderer.create(
<WithTrackingContext
onArticlePress={() => {}}
onPuzzlePress={onPuzzlePress}
section={edition.sections[3]}
stream={stream}
/>
);
const [link] = testInstance.root.findAllByType(Link);
link.props.onPress();
const [[call]] = stream.mock.calls;
expect(call).toMatchSnapshot();
expect(onPuzzlePress.mock.calls).toMatchSnapshot("onPuzzlePress");
});
};
// this test only applies for android as ios tiles have save buttons
export const saveClickTracking = () => {
it("Save/Unsave article click tracking", () => {
const edition = new MockEdition().get();
const stream = jest.fn();
const onArticlePress = jest.fn();
const artickleId = edition.sections[0].slices[0].lead.article.id;
const savedArticles = { artickleId: undefined };
const onArticleSavePress = () => {
savedArticles[artickleId] = !savedArticles[artickleId];
};
const testInstance = TestRenderer.create(
<SectionContext.Provider value={{ onArticleSavePress, savedArticles }}>
<WithTrackingContext
onArticlePress={onArticlePress}
onPuzzlePress={() => {}}
section={edition.sections[0]}
stream={stream}
/>
</SectionContext.Provider>
);
const [starButton] = testInstance.root.findAllByType(StarButton);
starButton.props.onPress();
starButton.props.onPress();
const call = stream.mock.calls;
expect(call).toMatchSnapshot();
});
};