@@ -15,82 +15,120 @@ void main() {
15
15
fixture = Fixture ();
16
16
});
17
17
18
- test ('didPush generates a new trace' , () {
19
- final fromRoute = _route (RouteSettings (name: 'From Route' ));
20
- final toRoute = _route (RouteSettings (name: 'To Route' ));
21
- final oldTraceId = fixture.hub.scope.propagationContext.traceId;
18
+ group ('SentryNavigatorObserver' , () {
19
+ group ('when starting traces on navigation is enabled (default)' , () {
20
+ test ('didPush should start a new trace' , () {
21
+ final from = _route (RouteSettings (name: 'From Route' ));
22
+ final to = _route (RouteSettings (name: 'To Route' ));
23
+ final before = fixture.hub.scope.propagationContext.traceId;
24
+
25
+ fixture.getSut ().didPush (to, from);
26
+
27
+ final after = fixture.hub.scope.propagationContext.traceId;
28
+ expect (after, isNot (before));
29
+ });
30
+
31
+ test ('didPop should start a new trace' , () {
32
+ final from = _route (RouteSettings (name: 'From Route' ));
33
+ final to = _route (RouteSettings (name: 'To Route' ));
34
+ final before = fixture.hub.scope.propagationContext.traceId;
35
+
36
+ fixture.getSut ().didPop (to, from);
37
+
38
+ final after = fixture.hub.scope.propagationContext.traceId;
39
+ expect (after, isNot (before));
40
+ });
41
+
42
+ test ('didReplace should start a new trace' , () {
43
+ final from = _route (RouteSettings (name: 'From Route' ));
44
+ final to = _route (RouteSettings (name: 'To Route' ));
45
+ final before = fixture.hub.scope.propagationContext.traceId;
46
+
47
+ fixture.getSut ().didReplace (newRoute: to, oldRoute: from);
48
+
49
+ final after = fixture.hub.scope.propagationContext.traceId;
50
+ expect (after, isNot (before));
51
+ });
52
+
53
+ group ('execution order' , () {
54
+ void _stubHub () {
55
+ when (fixture.mockHub.generateNewTrace ()).thenReturn (null );
56
+ when (fixture.mockHub.configureScope (any))
57
+ .thenAnswer ((_) => Future .value ());
58
+ when (fixture.mockHub.startTransactionWithContext (
59
+ any,
60
+ bindToScope: anyNamed ('bindToScope' ),
61
+ waitForChildren: anyNamed ('waitForChildren' ),
62
+ autoFinishAfter: anyNamed ('autoFinishAfter' ),
63
+ trimEnd: anyNamed ('trimEnd' ),
64
+ onFinish: anyNamed ('onFinish' ),
65
+ customSamplingContext: anyNamed ('customSamplingContext' ),
66
+ startTimestamp: anyNamed ('startTimestamp' ),
67
+ )).thenReturn (NoOpSentrySpan ());
68
+ }
69
+
70
+ test (
71
+ 'didPush should call generateNewTrace before starting the transaction' ,
72
+ () {
73
+ final from = _route (RouteSettings (name: 'From Route' ));
74
+ final to = _route (RouteSettings (name: 'To Route' ));
75
+
76
+ _stubHub ();
77
+ final sut = fixture.getSut (hub: fixture.mockHub);
78
+ sut.didPush (to, from);
79
+
80
+ verifyInOrder ([
81
+ fixture.mockHub.generateNewTrace (),
82
+ fixture.mockHub.startTransactionWithContext (
83
+ any,
84
+ bindToScope: anyNamed ('bindToScope' ),
85
+ waitForChildren: anyNamed ('waitForChildren' ),
86
+ autoFinishAfter: anyNamed ('autoFinishAfter' ),
87
+ trimEnd: anyNamed ('trimEnd' ),
88
+ onFinish: anyNamed ('onFinish' ),
89
+ customSamplingContext: anyNamed ('customSamplingContext' ),
90
+ startTimestamp: anyNamed ('startTimestamp' ),
91
+ ),
92
+ ]);
93
+ });
94
+ });
95
+ });
22
96
23
- final sut = fixture.getSut ();
24
- sut.didPush (toRoute, fromRoute);
97
+ group ('when starting traces on navigation is disabled' , () {
98
+ test ('didPush should not start a new trace' , () {
99
+ final from = _route (RouteSettings (name: 'From Route' ));
100
+ final to = _route (RouteSettings (name: 'To Route' ));
101
+ final before = fixture.hub.scope.propagationContext.traceId;
25
102
26
- final newTraceId = fixture.hub.scope.propagationContext.traceId;
27
- expect (oldTraceId, isNot (newTraceId));
28
- });
103
+ fixture.getSut (enableNewTraceOnNavigation: false ).didPush (to, from);
29
104
30
- test ('didPop generates a new trace' , () {
31
- final fromRoute = _route (RouteSettings (name: 'From Route' ));
32
- final toRoute = _route (RouteSettings (name: 'To Route' ));
33
- final oldTraceId = fixture.hub.scope.propagationContext.traceId;
105
+ final after = fixture.hub.scope.propagationContext.traceId;
106
+ expect (after, equals (before));
107
+ });
34
108
35
- final sut = fixture.getSut ();
36
- sut.didPop (toRoute, fromRoute);
109
+ test ('didPop should not start a new trace' , () {
110
+ final from = _route (RouteSettings (name: 'From Route' ));
111
+ final to = _route (RouteSettings (name: 'To Route' ));
112
+ final before = fixture.hub.scope.propagationContext.traceId;
37
113
38
- final newTraceId = fixture.hub.scope.propagationContext.traceId;
39
- expect (oldTraceId, isNot (newTraceId));
40
- });
114
+ fixture.getSut (enableNewTraceOnNavigation: false ).didPop (to, from);
41
115
42
- test ('didReplace generates a new trace' , () {
43
- final fromRoute = _route (RouteSettings (name: 'From Route' ));
44
- final toRoute = _route (RouteSettings (name: 'To Route' ));
45
- final oldTraceId = fixture.hub.scope.propagationContext.traceId;
116
+ final after = fixture.hub.scope.propagationContext.traceId;
117
+ expect (after, equals (before));
118
+ });
46
119
47
- final sut = fixture.getSut ();
48
- sut.didReplace (newRoute: toRoute, oldRoute: fromRoute);
120
+ test ('didReplace should not start a new trace' , () {
121
+ final from = _route (RouteSettings (name: 'From Route' ));
122
+ final to = _route (RouteSettings (name: 'To Route' ));
123
+ final before = fixture.hub.scope.propagationContext.traceId;
49
124
50
- final newTraceId = fixture.hub.scope.propagationContext.traceId;
51
- expect (oldTraceId, isNot (newTraceId));
52
- } );
125
+ fixture
126
+ . getSut (enableNewTraceOnNavigation : false )
127
+ . didReplace (newRoute : to, oldRoute : from );
53
128
54
- group ('execution order' , () {
55
- /// Prepares mocks, we don't care about what they exactly do.
56
- /// We only test the order of execution in this group.
57
- void _prepareMocks () {
58
- when (fixture.mockHub.generateNewTrace ()).thenAnswer ((_) => {});
59
- when (fixture.mockHub.configureScope (any))
60
- .thenAnswer ((_) => Future .value ());
61
- when (fixture.mockHub.startTransactionWithContext (
62
- any,
63
- bindToScope: anyNamed ('bindToScope' ),
64
- waitForChildren: anyNamed ('waitForChildren' ),
65
- autoFinishAfter: anyNamed ('autoFinishAfter' ),
66
- trimEnd: anyNamed ('trimEnd' ),
67
- onFinish: anyNamed ('onFinish' ),
68
- customSamplingContext: anyNamed ('customSamplingContext' ),
69
- startTimestamp: anyNamed ('startTimestamp' ),
70
- )).thenReturn (NoOpSentrySpan ());
71
- }
72
-
73
- test ('didPush generates a new trace before creating transaction spans' , () {
74
- final fromRoute = _route (RouteSettings (name: 'From Route' ));
75
- final toRoute = _route (RouteSettings (name: 'To Route' ));
76
-
77
- _prepareMocks ();
78
-
79
- final sut = fixture.getSut (hub: fixture.mockHub);
80
- sut.didPush (toRoute, fromRoute);
81
- verifyInOrder ([
82
- fixture.mockHub.generateNewTrace (),
83
- fixture.mockHub.startTransactionWithContext (
84
- any,
85
- bindToScope: anyNamed ('bindToScope' ),
86
- waitForChildren: anyNamed ('waitForChildren' ),
87
- autoFinishAfter: anyNamed ('autoFinishAfter' ),
88
- trimEnd: anyNamed ('trimEnd' ),
89
- onFinish: anyNamed ('onFinish' ),
90
- customSamplingContext: anyNamed ('customSamplingContext' ),
91
- startTimestamp: anyNamed ('startTimestamp' ),
92
- ),
93
- ]);
129
+ final after = fixture.hub.scope.propagationContext.traceId;
130
+ expect (after, equals (before));
131
+ });
94
132
});
95
133
});
96
134
}
@@ -105,11 +143,17 @@ class Fixture {
105
143
late final mockHub = MockHub ();
106
144
late final hub = Hub (options);
107
145
108
- SentryNavigatorObserver getSut ({Hub ? hub}) {
146
+ SentryNavigatorObserver getSut ({
147
+ Hub ? hub,
148
+ bool enableNewTraceOnNavigation = true ,
149
+ }) {
109
150
hub ?? = this .hub;
110
151
if (hub == mockHub) {
111
152
when (mockHub.options).thenReturn (options);
112
153
}
113
- return SentryNavigatorObserver (hub: hub);
154
+ return SentryNavigatorObserver (
155
+ hub: hub,
156
+ enableNewTraceOnNavigation: enableNewTraceOnNavigation,
157
+ );
114
158
}
115
159
}
0 commit comments