Commit 5534fdcf authored by Mark Florian's avatar Mark Florian

Merge branch 'update-avatar-helper-for-graphql' into 'master'

Update avatar_helper to accept GraphQL id

See merge request gitlab-org/gitlab!32261
parents eb33305a 776b502e
import { escape } from 'lodash'; import { escape } from 'lodash';
import { getFirstCharacterCapitalized } from '~/lib/utils/text_utility'; import { getFirstCharacterCapitalized } from '~/lib/utils/text_utility';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
export const DEFAULT_SIZE_CLASS = 's40'; export const DEFAULT_SIZE_CLASS = 's40';
export const IDENTICON_BG_COUNT = 7; export const IDENTICON_BG_COUNT = 7;
export function getIdenticonBackgroundClass(entityId) { export function getIdenticonBackgroundClass(entityId) {
const type = (entityId % IDENTICON_BG_COUNT) + 1; // If a GraphQL string id is passed in, convert it to the entity number
const id = typeof entityId === 'string' ? getIdFromGraphQLId(entityId) : entityId;
const type = (id % IDENTICON_BG_COUNT) + 1;
return `bg${type}`; return `bg${type}`;
} }
......
...@@ -4,7 +4,7 @@ import { getIdenticonBackgroundClass, getIdenticonTitle } from '~/helpers/avatar ...@@ -4,7 +4,7 @@ import { getIdenticonBackgroundClass, getIdenticonTitle } from '~/helpers/avatar
export default { export default {
props: { props: {
entityId: { entityId: {
type: Number, type: [Number, String],
required: true, required: true,
}, },
entityName: { entityName: {
......
...@@ -15,10 +15,22 @@ function matchAll(str) { ...@@ -15,10 +15,22 @@ function matchAll(str) {
describe('avatar_helper', () => { describe('avatar_helper', () => {
describe('getIdenticonBackgroundClass', () => { describe('getIdenticonBackgroundClass', () => {
it('returns identicon bg class from id', () => { it('returns identicon bg class from id that is a number', () => {
expect(getIdenticonBackgroundClass(1)).toEqual('bg2'); expect(getIdenticonBackgroundClass(1)).toEqual('bg2');
}); });
it('returns identicon bg class from id that is a string', () => {
expect(getIdenticonBackgroundClass('1')).toEqual('bg2');
});
it('returns identicon bg class from id that is a GraphQL string id', () => {
expect(getIdenticonBackgroundClass('gid://gitlab/Project/1')).toEqual('bg2');
});
it('returns identicon bg class from unparsable string', () => {
expect(getIdenticonBackgroundClass('gid://gitlab/')).toEqual('bg1');
});
it(`wraps around if id is bigger than ${IDENTICON_BG_COUNT}`, () => { it(`wraps around if id is bigger than ${IDENTICON_BG_COUNT}`, () => {
expect(getIdenticonBackgroundClass(IDENTICON_BG_COUNT + 4)).toEqual('bg5'); expect(getIdenticonBackgroundClass(IDENTICON_BG_COUNT + 4)).toEqual('bg5');
expect(getIdenticonBackgroundClass(IDENTICON_BG_COUNT * 5 + 6)).toEqual('bg7'); expect(getIdenticonBackgroundClass(IDENTICON_BG_COUNT * 5 + 6)).toEqual('bg7');
......
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Identicon matches snapshot 1`] = ` exports[`Identicon entity id is a GraphQL id matches snapshot 1`] = `
<div
class="avatar identicon s40 bg2"
>
E
</div>
`;
exports[`Identicon entity id is a number matches snapshot 1`] = `
<div <div
class="avatar identicon s40 bg2" class="avatar identicon s40 bg2"
> >
......
...@@ -4,12 +4,17 @@ import IdenticonComponent from '~/vue_shared/components/identicon.vue'; ...@@ -4,12 +4,17 @@ import IdenticonComponent from '~/vue_shared/components/identicon.vue';
describe('Identicon', () => { describe('Identicon', () => {
let wrapper; let wrapper;
const createComponent = () => { const defaultProps = {
wrapper = shallowMount(IdenticonComponent, {
propsData: {
entityId: 1, entityId: 1,
entityName: 'entity-name', entityName: 'entity-name',
sizeClass: 's40', sizeClass: 's40',
};
const createComponent = (props = {}) => {
wrapper = shallowMount(IdenticonComponent, {
propsData: {
...defaultProps,
...props,
}, },
}); });
}; };
...@@ -19,15 +24,27 @@ describe('Identicon', () => { ...@@ -19,15 +24,27 @@ describe('Identicon', () => {
wrapper = null; wrapper = null;
}); });
it('matches snapshot', () => { describe('entity id is a number', () => {
createComponent(); beforeEach(createComponent);
it('matches snapshot', () => {
expect(wrapper.element).toMatchSnapshot(); expect(wrapper.element).toMatchSnapshot();
}); });
it('adds a correct class to identicon', () => { it('adds a correct class to identicon', () => {
createComponent(); expect(wrapper.find({ ref: 'identicon' }).classes()).toContain('bg2');
});
});
describe('entity id is a GraphQL id', () => {
beforeEach(() => createComponent({ entityId: 'gid://gitlab/Project/8' }));
it('matches snapshot', () => {
expect(wrapper.element).toMatchSnapshot();
});
it('adds a correct class to identicon', () => {
expect(wrapper.find({ ref: 'identicon' }).classes()).toContain('bg2'); expect(wrapper.find({ ref: 'identicon' }).classes()).toContain('bg2');
}); });
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment