-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathCallNodeContextMenu.test.js
167 lines (147 loc) · 5.86 KB
/
CallNodeContextMenu.test.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import * as React from 'react';
import { Provider } from 'react-redux';
import copy from 'copy-to-clipboard';
import { render } from 'firefox-profiler/test/fixtures/testing-library';
import { CallNodeContextMenu } from '../../components/shared/CallNodeContextMenu';
import { storeWithProfile } from '../fixtures/stores';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import {
changeRightClickedCallNode,
changeExpandedCallNodes,
setContextMenuVisibility,
} from '../../actions/profile-view';
import { selectedThreadSelectors } from '../../selectors/per-thread';
import { ensureExists } from '../../utils/flow';
import { fireFullClick } from '../fixtures/utils';
describe('calltree/CallNodeContextMenu', function() {
// Provide a store with a useful profile to assert context menu operations off of.
function createStore() {
// Create a profile that every transform can be applied to.
const {
profile,
funcNamesDictPerThread: [{ A, B }],
} = getProfileFromTextSamples(`
A A A
B[lib:XUL] B[lib:XUL] B[lib:XUL]
B[lib:XUL] B[lib:XUL] B[lib:XUL]
B[lib:XUL] B[lib:XUL] B[lib:XUL]
C C H
D F I
E E
`);
const store = storeWithProfile(profile);
store.dispatch(changeExpandedCallNodes(0, [[A]]));
store.dispatch(changeRightClickedCallNode(0, [A, B]));
return store;
}
function setup(store = createStore(), openMenuState = true) {
store.dispatch(setContextMenuVisibility(openMenuState));
const renderResult = render(
<Provider store={store}>
<CallNodeContextMenu />
</Provider>
);
return { ...renderResult, getState: store.getState };
}
describe('basic rendering', function() {
it('does not render the context menu when it is closed', () => {
const isContextMenuOpen = false;
const { container } = setup(createStore(), isContextMenuOpen);
expect(container.querySelector('.react-contextmenu')).toBeNull();
});
it('renders a full context menu when open, with many nav items', () => {
const isContextMenuOpen = true;
const { container } = setup(createStore(), isContextMenuOpen);
expect(
ensureExists(
container.querySelector('.react-contextmenu'),
`Couldn't find the context menu root component .react-contextmenu`
).children.length > 1
).toBeTruthy();
expect(container.firstChild).toMatchSnapshot();
});
});
describe('clicking on call tree transforms', function() {
// Iterate through each transform slug, and click things in it.
const fixtures = [
{ matcher: /Merge function/, type: 'merge-function' },
{ matcher: /Merge node only/, type: 'merge-call-node' },
{ matcher: /Focus on subtree only/, type: 'focus-subtree' },
{ matcher: /Focus on function/, type: 'focus-function' },
{ matcher: /Collapse function/, type: 'collapse-function-subtree' },
{ matcher: /XUL/, type: 'collapse-resource' },
{
matcher: /Collapse direct recursion/,
type: 'collapse-direct-recursion',
},
{ matcher: /Drop samples/, type: 'drop-function' },
];
fixtures.forEach(({ matcher, type }) => {
it(`adds a transform for "${type}"`, function() {
const { getState, getByText } = setup();
fireFullClick(getByText(matcher));
expect(
selectedThreadSelectors.getTransformStack(getState())[0].type
).toBe(type);
});
});
});
describe('clicking on the rest of the menu items', function() {
it('can expand all call nodes in the call tree', function() {
const { getState, getByText } = setup();
expect(
selectedThreadSelectors.getExpandedCallNodeIndexes(getState())
).toHaveLength(1);
fireFullClick(getByText('Expand all'));
// This test only asserts that a bunch of call nodes were actually expanded.
expect(
selectedThreadSelectors.getExpandedCallNodeIndexes(getState())
).toHaveLength(11);
});
it('can look up functions on SearchFox', function() {
const { getByText } = setup();
jest.spyOn(window, 'open').mockImplementation(() => {});
fireFullClick(getByText(/Searchfox/));
expect(window.open).toBeCalledWith(
'https://searchfox.org/mozilla-central/search?q=B',
'_blank'
);
});
it('can copy a function name', function() {
const { getByText } = setup();
// Copy is a mocked module, clear it both before and after.
fireFullClick(getByText('Copy function name'));
expect(copy).toBeCalledWith('B');
});
it('can copy a script URL', function() {
// Create a new profile that has JavaScript in it.
const {
profile,
funcNamesPerThread: [funcNames],
} = getProfileFromTextSamples(`
A.js
`);
const funcIndex = funcNames.indexOf('A.js');
const [thread] = profile.threads;
thread.funcTable.fileName[funcIndex] = thread.stringTable.indexForString(
'https://example.com/script.js'
);
const store = storeWithProfile(profile);
store.dispatch(changeRightClickedCallNode(0, [funcIndex]));
const { getByText } = setup(store);
// Copy is a mocked module, clear it both before and after.
fireFullClick(getByText('Copy script URL'));
expect(copy).toBeCalledWith('https://example.com/script.js');
});
it('can copy a stack', function() {
const { getByText } = setup();
// Copy is a mocked module, clear it both before and after.
fireFullClick(getByText('Copy stack'));
expect(copy).toBeCalledWith(`B\nA\n`);
});
});
});