Skip to content

Commit 8fe4946

Browse files
committed
add more tests
1 parent 5767947 commit 8fe4946

File tree

5 files changed

+237
-36
lines changed

5 files changed

+237
-36
lines changed

WORKSPACE

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ load("@io_bazel_rules_webtesting//web:repositories.bzl", "web_test_repositories"
3737

3838
web_test_repositories(omit_bazel_skylib = True)
3939

40+
# rules_python has to be placed before load("@io_bazel_rules_closure//closure:repositories.bzl")
41+
# in the dependencies list, otherwise we get "cannot load '@rules_python//python:py_xxx.bzl': no such file"
42+
http_archive(
43+
name = "rules_python",
44+
sha256 = "0a8003b044294d7840ac7d9d73eef05d6ceb682d7516781a4ec62eeb34702578",
45+
strip_prefix = "rules_python-0.24.0",
46+
urls = [
47+
"http://mirror.tensorflow.org/github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz",
48+
"https://github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz", # 2023-07-11
49+
],
50+
)
51+
4052
load("@io_bazel_rules_webtesting//web:py_repositories.bzl", "py_repositories")
4153

4254
py_repositories()

tensorboard/webapp/metrics/effects/card_interaction_effects.ts

+29-27
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,38 @@ export class CardInteractionEffects implements OnInitEffects {
9191
this.getPreviousCardInteractions$,
9292
this.store.select(getCardMetadataMap)
9393
),
94-
tap(([, cardInteractions, previousCardInteractions, metadataMap]) => {
95-
const nextCardInteractions = {
96-
pins: makeUnique([
97-
...cardInteractions.pins,
98-
...previousCardInteractions.pins,
99-
]),
100-
clicks: makeUnique([
101-
...cardInteractions.clicks,
102-
...previousCardInteractions.clicks,
103-
]),
104-
tagFilters: Array.from(
105-
new Set([
106-
...cardInteractions.tagFilters,
107-
...previousCardInteractions.tagFilters,
108-
])
109-
),
110-
};
94+
tap(
95+
([, newCardInteractions, previousCardInteractions, metadataMap]) => {
96+
const nextCardInteractions = {
97+
pins: makeUnique([
98+
...previousCardInteractions.pins,
99+
...newCardInteractions.pins,
100+
]),
101+
clicks: makeUnique([
102+
...previousCardInteractions.clicks,
103+
...newCardInteractions.clicks,
104+
]),
105+
tagFilters: Array.from(
106+
new Set([
107+
...previousCardInteractions.tagFilters,
108+
...newCardInteractions.tagFilters,
109+
])
110+
),
111+
};
111112

112-
this.store.dispatch(
113-
metricsPreviousCardInteractionsChanged({
114-
cardInteractions: nextCardInteractions,
115-
})
116-
);
113+
this.store.dispatch(
114+
metricsPreviousCardInteractionsChanged({
115+
cardInteractions: nextCardInteractions,
116+
})
117+
);
117118

118-
function makeUnique(cardMetadata: CardIdWithMetadata[]) {
119-
return Array.from(
120-
new Set(cardMetadata.map(({cardId}) => cardId))
121-
).map((cardId) => ({...metadataMap[cardId], cardId}));
119+
function makeUnique(cardMetadata: CardIdWithMetadata[]) {
120+
return Array.from(
121+
new Set(cardMetadata.map(({cardId}) => cardId))
122+
).map((cardId) => ({...metadataMap[cardId], cardId}));
123+
}
122124
}
123-
})
125+
)
124126
);
125127
},
126128
{dispatch: false}

tensorboard/webapp/metrics/effects/card_interactions_effects_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ describe('CardInteractions Effects', () => {
201201
expect(dispatchedActions).toEqual([
202202
actions.metricsPreviousCardInteractionsChanged({
203203
cardInteractions: {
204-
pins: [card1a, card2a],
205-
clicks: [card1b, card2b],
206-
tagFilters: ['foo', 'bar'],
204+
pins: [card2a, card1a],
205+
clicks: [card2b, card1b],
206+
tagFilters: ['bar', 'foo'],
207207
},
208208
}),
209209
]);

tensorboard/webapp/metrics/store/metrics_reducers.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ const reducer = createReducer(
775775
return {
776776
...state,
777777
tagFilter,
778-
cardInteractions: nextNewCardInteractions,
778+
newCardInteractions: nextNewCardInteractions,
779779
};
780780
}),
781781
on(actions.metricsChangeTooltipSort, (state, {sort}) => {
@@ -1192,7 +1192,7 @@ const reducer = createReducer(
11921192
cardToPinnedCopy: nextCardToPinnedCopy,
11931193
cardToPinnedCopyCache: nextCardToPinnedCopyCache,
11941194
pinnedCardToOriginal: nextPinnedCardToOriginal,
1195-
cardInteractions: nextNewCardInteractions,
1195+
newCardInteractions: nextNewCardInteractions,
11961196
previousCardInteractions: nextPreviousCardInteractions,
11971197
};
11981198
}),
@@ -1539,10 +1539,10 @@ const reducer = createReducer(
15391539
}
15401540
),
15411541
on(actions.metricsCardClicked, (state, {cardId}) => {
1542-
const nextClicksIds = new Set(
1543-
state.newCardInteractions.clicks.map(({cardId}) => cardId)
1544-
);
1545-
nextClicksIds.add(cardId);
1542+
const nextClicksIds = state.newCardInteractions.clicks
1543+
.map(({cardId}) => cardId)
1544+
.filter((id) => id !== cardId)
1545+
.concat(cardId);
15461546

15471547
const nextNewCardInteractions = {
15481548
...state.newCardInteractions,

tensorboard/webapp/metrics/store/metrics_reducers_test.ts

+187
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
TagMetadata as DataSourceTagMetadata,
2929
} from '../data_source';
3030
import {
31+
appStateFromMetricsState,
3132
buildDataSourceTagMetadata,
3233
buildMetricsSettingsState,
3334
buildMetricsState,
@@ -2938,6 +2939,53 @@ describe('metrics reducers', () => {
29382939
return reducers(beforeState, action);
29392940
}).toThrow();
29402941
});
2942+
2943+
it('adds an entry to newCardInteractions', () => {
2944+
const state = buildMetricsState({
2945+
cardMetadataMap: {
2946+
card1: {
2947+
tag: 'foo',
2948+
runId: null,
2949+
plugin: PluginType.SCALARS,
2950+
},
2951+
},
2952+
});
2953+
const state2 = reducers(
2954+
state,
2955+
actions.cardPinStateToggled({
2956+
cardId: 'card1',
2957+
wasPinned: false,
2958+
canCreateNewPins: true,
2959+
})
2960+
);
2961+
2962+
expect(state2.newCardInteractions.pins).toEqual([
2963+
{
2964+
cardId: 'card1',
2965+
plugin: PluginType.SCALARS,
2966+
runId: null,
2967+
tag: 'foo',
2968+
},
2969+
]);
2970+
2971+
const state3 = reducers(
2972+
state2,
2973+
actions.cardPinStateToggled({
2974+
cardId: 'card1',
2975+
wasPinned: false,
2976+
canCreateNewPins: true,
2977+
})
2978+
);
2979+
2980+
expect(state3.newCardInteractions.pins).toEqual([
2981+
{
2982+
cardId: 'card1',
2983+
plugin: PluginType.SCALARS,
2984+
runId: null,
2985+
tag: 'foo',
2986+
},
2987+
]);
2988+
});
29412989
});
29422990

29432991
describe('metricsCardStateUpdated', () => {
@@ -3042,6 +3090,20 @@ describe('metrics reducers', () => {
30423090
const nextState = reducers(state, action);
30433091
expect(nextState.tagFilter).toBe('foobar');
30443092
});
3093+
3094+
it('adds entry to newCardInteractions', () => {
3095+
const state = buildMetricsState({});
3096+
const state2 = reducers(
3097+
state,
3098+
actions.metricsTagFilterChanged({tagFilter: 'foo'})
3099+
);
3100+
expect(state2.newCardInteractions.tagFilters).toEqual(['foo']);
3101+
const state3 = reducers(
3102+
state2,
3103+
actions.metricsTagFilterChanged({tagFilter: 'foo'})
3104+
);
3105+
expect(state3.newCardInteractions.tagFilters).toEqual(['foo']);
3106+
});
30453107
});
30463108

30473109
describe('metricsTagGroupExpansionChanged', () => {
@@ -4717,4 +4779,129 @@ describe('metrics reducers', () => {
47174779
});
47184780
});
47194781
});
4782+
4783+
describe('#metricsPreviousCardInteractionsChanged', () => {
4784+
it('sets previousCardInteractions', () => {
4785+
const cardInteractions = {
4786+
pins: [
4787+
{
4788+
cardId: 'card1',
4789+
runId: null,
4790+
plugin: PluginType.SCALARS,
4791+
tag: 'bar',
4792+
},
4793+
],
4794+
clicks: [
4795+
{
4796+
cardId: 'card2',
4797+
runId: null,
4798+
plugin: PluginType.SCALARS,
4799+
tag: 'baz',
4800+
},
4801+
],
4802+
tagFilters: ['foo'],
4803+
};
4804+
const state = buildMetricsState({});
4805+
const state2 = reducers(
4806+
state,
4807+
actions.metricsPreviousCardInteractionsChanged({
4808+
cardInteractions,
4809+
})
4810+
);
4811+
4812+
expect(state2.previousCardInteractions).toEqual(cardInteractions);
4813+
});
4814+
});
4815+
4816+
describe('#metricsCardClicked', () => {
4817+
it('adds entry to newCardInteractions', () => {
4818+
const state = buildMetricsState({
4819+
cardMetadataMap: {
4820+
card1: {
4821+
plugin: PluginType.SCALARS,
4822+
tag: 'foo',
4823+
runId: null,
4824+
},
4825+
card2: {
4826+
plugin: PluginType.SCALARS,
4827+
tag: 'foo',
4828+
runId: null,
4829+
},
4830+
},
4831+
});
4832+
const state2 = reducers(
4833+
state,
4834+
actions.metricsCardClicked({cardId: 'card1'})
4835+
);
4836+
expect(state2.newCardInteractions.clicks).toEqual([
4837+
{
4838+
cardId: 'card1',
4839+
plugin: PluginType.SCALARS,
4840+
tag: 'foo',
4841+
runId: null,
4842+
},
4843+
]);
4844+
const state3 = reducers(
4845+
state2,
4846+
actions.metricsCardClicked({cardId: 'card2'})
4847+
);
4848+
expect(state3.newCardInteractions.clicks).toEqual([
4849+
{
4850+
cardId: 'card1',
4851+
plugin: PluginType.SCALARS,
4852+
tag: 'foo',
4853+
runId: null,
4854+
},
4855+
{
4856+
cardId: 'card2',
4857+
plugin: PluginType.SCALARS,
4858+
tag: 'foo',
4859+
runId: null,
4860+
},
4861+
]);
4862+
});
4863+
4864+
it('cannot add duplicate entries to newCardInteractions', () => {
4865+
const state = buildMetricsState({
4866+
cardMetadataMap: {
4867+
card1: {
4868+
plugin: PluginType.SCALARS,
4869+
tag: 'foo',
4870+
runId: null,
4871+
},
4872+
card2: {
4873+
plugin: PluginType.SCALARS,
4874+
tag: 'foo',
4875+
runId: null,
4876+
},
4877+
},
4878+
});
4879+
const state2 = reducers(
4880+
state,
4881+
actions.metricsCardClicked({cardId: 'card1'})
4882+
);
4883+
const state3 = reducers(
4884+
state2,
4885+
actions.metricsCardClicked({cardId: 'card2'})
4886+
);
4887+
const state4 = reducers(
4888+
state3,
4889+
actions.metricsCardClicked({cardId: 'card1'})
4890+
);
4891+
expect(state4.newCardInteractions.clicks).toEqual([
4892+
{
4893+
cardId: 'card2',
4894+
plugin: PluginType.SCALARS,
4895+
tag: 'foo',
4896+
runId: null,
4897+
},
4898+
{
4899+
cardId: 'card1',
4900+
plugin: PluginType.SCALARS,
4901+
tag: 'foo',
4902+
runId: null,
4903+
},
4904+
]);
4905+
});
4906+
});
47204907
});

0 commit comments

Comments
 (0)