Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
178720b7
Unverified
Commit
178720b7
authored
3 years ago
by
Anton Kulyk
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Extract `getInitialCollectionId` selector helpers, add unit tests (#16867)
parent
2a34dc56
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend/src/metabase/entities/collections.js
+59
-9
59 additions, 9 deletions
frontend/src/metabase/entities/collections.js
frontend/test/metabase/entities/collections/selectors.unit.spec.js
+207
-0
207 additions, 0 deletions
...test/metabase/entities/collections/selectors.unit.spec.js
with
266 additions
and
9 deletions
frontend/src/metabase/entities/collections.js
+
59
−
9
View file @
178720b7
...
...
@@ -101,15 +101,14 @@ const Collections = createEntity({
getInitialCollectionId
:
createSelector
(
[
state
=>
state
.
entities
.
collections
,
// these are listed in order of priority
(
state
,
{
collectionId
})
=>
collectionId
,
(
state
,
{
params
})
=>
(
params
?
params
.
collectionId
:
undefined
),
(
state
,
{
params
,
location
})
=>
params
&&
location
&&
Urls
.
isCollectionPath
(
location
.
pathname
)
?
Urls
.
extractCollectionId
(
params
.
slug
)
:
undefined
,
(
state
,
{
location
})
=>
location
&&
location
.
query
?
location
.
query
.
collectionId
:
undefined
,
byCollectionIdProp
,
byCollectionIdNavParam
,
byCollectionUrlId
,
byCollectionQueryParameter
,
// defaults
()
=>
ROOT_COLLECTION
.
id
,
getUserPersonalCollectionId
,
],
...
...
@@ -120,7 +119,7 @@ const Collections = createEntity({
return
canonicalCollectionId
(
collectionId
);
}
}
return
null
;
return
canonicalCollectionId
(
ROOT_COLLECTION
.
id
)
;
},
),
},
...
...
@@ -306,3 +305,54 @@ export function getExpandedCollectionsById(
return
collectionsById
;
}
// Initial collection ID selector helpers
/**
* @param {ReduxState} state
* @param {{collectionId?: number}} props
* @returns {number | undefined}
*/
function
byCollectionIdProp
(
state
,
{
collectionId
})
{
return
collectionId
;
}
/**
* @param {ReduxState} state
* @param {params?: {collectionId?: number}} props
* @returns {number | undefined}
*/
function
byCollectionIdNavParam
(
state
,
{
params
})
{
return
params
&&
params
.
collectionId
;
}
/**
* Extracts ID from collection URL slugs
*
* Example: /collection/14-marketing —> 14
*
* @param {ReduxState} state
* @param {params?: {slug?: string}, location?: {pathname?: string}} props
* @returns {number | undefined}
*/
function
byCollectionUrlId
(
state
,
{
params
,
location
})
{
const
isCollectionPath
=
params
&&
params
.
slug
&&
location
&&
Urls
.
isCollectionPath
(
location
.
pathname
);
return
isCollectionPath
&&
Urls
.
extractCollectionId
(
params
.
slug
);
}
/**
* Extracts collection ID from query params
*
* Example: /some-route?collectionId=14 —> 14
*
* @param {ReduxState} state
* @param {location?: {query?: {collectionId?: number}}} props
* @returns {number | undefined}
*/
function
byCollectionQueryParameter
(
state
,
{
location
})
{
return
location
&&
location
.
query
&&
location
.
query
.
collectionId
;
}
This diff is collapsed.
Click to expand it.
frontend/test/metabase/entities/collections/selectors.unit.spec.js
0 → 100644
+
207
−
0
View file @
178720b7
import
Collections
from
"
metabase/entities/collections
"
;
describe
(
"
Collection selectors
"
,
()
=>
{
const
CANONICAL_ROOT_COLLECTION_ID
=
null
;
const
PERSONAL_COLLECTION
=
{
id
:
1
,
name
:
"
My personal collection
"
,
can_write
:
true
,
};
const
TEST_COLLECTION
=
{
id
:
2
,
name
:
"
Writable Collection
"
,
can_write
:
true
,
};
const
TEST_COLLECTION_2
=
{
id
:
3
,
name
:
"
One More Writable Collection
"
,
can_write
:
true
,
};
const
TEST_READ_ONLY_COLLECTION
=
{
id
:
4
,
name
:
"
Read-only Collection
"
,
can_write
:
false
,
};
const
DEFAULT_COLLECTIONS
=
{
[
TEST_COLLECTION
.
id
]:
TEST_COLLECTION
,
[
TEST_COLLECTION_2
.
id
]:
TEST_COLLECTION_2
,
[
TEST_READ_ONLY_COLLECTION
.
id
]:
TEST_READ_ONLY_COLLECTION
,
};
function
getReduxState
({
isAdmin
=
false
,
collections
=
DEFAULT_COLLECTIONS
,
}
=
{})
{
return
{
currentUser
:
{
personal_collection_id
:
PERSONAL_COLLECTION
.
id
,
},
entities
:
{
collections
:
{
...
collections
,
root
:
{
id
:
"
root
"
,
name
:
"
Our analytics
"
,
can_write
:
isAdmin
,
},
[
PERSONAL_COLLECTION
.
id
]:
PERSONAL_COLLECTION
,
},
},
};
}
describe
(
"
getInitialCollectionId
"
,
()
=>
{
const
{
getInitialCollectionId
}
=
Collections
.
selectors
;
const
state
=
getReduxState
();
it
(
"
suggests direct collectionId prop
"
,
()
=>
{
const
props
=
{
collectionId
:
TEST_COLLECTION
.
id
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
it
(
"
suggests collectionId navigation parameter
"
,
()
=>
{
const
props
=
{
params
:
{
collectionId
:
TEST_COLLECTION
.
id
,
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
it
(
"
suggests id from collection URL slug
"
,
()
=>
{
const
slug
=
`
${
TEST_COLLECTION
.
id
}
-writable-collection`
;
const
props
=
{
params
:
{
slug
,
},
location
:
{
pathname
:
`/collection/
${
slug
}
`
,
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
it
(
"
suggests collectionId query parameter
"
,
()
=>
{
const
props
=
{
location
:
{
query
:
{
collectionId
:
TEST_COLLECTION
.
id
,
},
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
it
(
"
fallbacks to root collection for admin users if can't suggest an id from props
"
,
()
=>
{
const
adminState
=
getReduxState
({
isAdmin
:
true
});
const
props
=
{};
expect
(
getInitialCollectionId
(
adminState
,
props
)).
toBe
(
CANONICAL_ROOT_COLLECTION_ID
,
);
});
it
(
"
fallbacks to personal collection for non-admin users if can't suggest an id from props
"
,
()
=>
{
const
props
=
{};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
PERSONAL_COLLECTION
.
id
);
});
it
(
"
does not use URL slug if it's not collection URL
"
,
()
=>
{
const
slug
=
`5-dashboard`
;
const
props
=
{
params
:
{
slug
,
},
location
:
{
pathname
:
`/dashboard/
${
slug
}
`
,
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
PERSONAL_COLLECTION
.
id
);
});
describe
(
"
order priority
"
,
()
=>
{
it
(
"
prioritizes direct collectionId prop
"
,
()
=>
{
const
props
=
{
collectionId
:
TEST_COLLECTION
.
id
,
params
:
{
collectionId
:
TEST_COLLECTION_2
.
id
,
slug
:
`
${
TEST_COLLECTION_2
.
id
}
-slug`
,
},
location
:
{
pathname
:
`/collection/
${
TEST_COLLECTION_2
.
id
}
-slug`
,
query
:
{
collectionId
:
TEST_COLLECTION_2
.
id
,
},
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
it
(
"
prioritizes collectionId navigation param
"
,
()
=>
{
const
props
=
{
params
:
{
collectionId
:
TEST_COLLECTION
.
id
,
slug
:
`
${
TEST_COLLECTION_2
.
id
}
-slug`
,
},
location
:
{
pathname
:
`/collection/
${
TEST_COLLECTION_2
.
id
}
-slug`
,
query
:
{
collectionId
:
TEST_COLLECTION_2
.
id
,
},
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
it
(
"
prioritizes id from a URL slug
"
,
()
=>
{
const
props
=
{
params
:
{
slug
:
`
${
TEST_COLLECTION
.
id
}
-slug`
,
},
location
:
{
pathname
:
`/collection/
${
TEST_COLLECTION
.
id
}
-slug`
,
query
:
{
collectionId
:
TEST_COLLECTION_2
.
id
,
},
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
});
describe
(
"
permissions
"
,
()
=>
{
it
(
"
does not suggest a read-only collection
"
,
()
=>
{
const
props
=
{
collectionId
:
TEST_READ_ONLY_COLLECTION
.
id
,
params
:
{
collectionId
:
TEST_READ_ONLY_COLLECTION
.
id
,
slug
:
`
${
TEST_READ_ONLY_COLLECTION
.
id
}
-slug`
,
},
location
:
{
pathname
:
`/collection/
${
TEST_READ_ONLY_COLLECTION
.
id
}
-slug`
,
query
:
{
collectionId
:
TEST_READ_ONLY_COLLECTION
.
id
,
},
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
PERSONAL_COLLECTION
.
id
,
);
});
it
(
"
picks writable collection even if read-only is prior
"
,
()
=>
{
const
props
=
{
collectionId
:
TEST_READ_ONLY_COLLECTION
.
id
,
params
:
{
collectionId
:
TEST_COLLECTION
.
id
,
},
};
expect
(
getInitialCollectionId
(
state
,
props
)).
toBe
(
TEST_COLLECTION
.
id
);
});
});
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment