Skip to content

Commit

Permalink
fix: assign key field for natures
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Dec 31, 2024
1 parent a0b688b commit be4e803
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 175 deletions.
4 changes: 4 additions & 0 deletions src/lib/assets/natures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,7 @@ export const natures = new Collection<string, PokemonTypes.Natures>([
}
]
]);

for (const [key, value] of natures.entries()) {
value.key = key;
}
1 change: 1 addition & 0 deletions src/lib/assets/pokemon-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export namespace PokemonTypes {
| 'All';

export interface Natures {
key?: string;
name: string;

increasedStat: string | null;
Expand Down
1 change: 1 addition & 0 deletions src/lib/mappers/natureMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { addPropertyToObjectFieldBased } from '#utils/addPropertyToObject';
export function mapNatureDataToNatureGraphQL({ data, requestedFields }: MapNatureDataToNatureGraphQLParameters): Nature {
const natureObject: Nature = {} as Nature;

addPropertyToObjectFieldBased({ objectTarget: natureObject, propertyKey: 'key', propertyValue: data.key, requestedFields });
addPropertyToObjectFieldBased({ objectTarget: natureObject, propertyKey: 'name', propertyValue: data.name, requestedFields });
addPropertyToObjectFieldBased({ objectTarget: natureObject, propertyKey: 'increasedStat', propertyValue: data.increasedStat, requestedFields });
addPropertyToObjectFieldBased({ objectTarget: natureObject, propertyKey: 'decreasedStat', propertyValue: data.decreasedStat, requestedFields });
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types/graphql-mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ export type {

/** A single Nature entry */
export interface Nature {
/** The key of the nature as stored in the API */
key: string;
/** The stat the nature decreases by 10% */
decreasedStat?: Maybe<string>;
/** The flavor of food the nature dislikes */
Expand Down
255 changes: 82 additions & 173 deletions tests/scenarios/natures/getAllNatures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { getAllNatures } from '#test-utils/queries/natures';
import { executeGraphQL } from '#test-utils/testUtils';

describe('getAllNatures', () => {
test('GIVEN a limit of 5 THEN returns the first 5 Natures', async () => {
test('GIVEN no parameters THEN returns all Natures', async () => {
const { data } = await executeGraphQL<'getAllNatures'>({
query: getAllNatures,
variables: { take: 5 }
query: getAllNatures
});

expect(data.getAllNatures).toEqual([
Expand All @@ -28,177 +27,87 @@ describe('getAllNatures', () => {
{
key: 'calm',
name: 'Calm'
},
{
key: 'careful',
name: 'Careful'
},
{
key: 'docile',
name: 'Docile'
},
{
key: 'gentle',
name: 'Gentle'
},
{
key: 'hardy',
name: 'Hardy'
},
{
key: 'hasty',
name: 'Hasty'
},
{
key: 'impish',
name: 'Impish'
},
{
key: 'jolly',
name: 'Jolly'
},
{
key: 'lax',
name: 'Lax'
},
{
key: 'lonely',
name: 'Lonely'
},
{
key: 'mild',
name: 'Mild'
},
{
key: 'modest',
name: 'Modest'
},
{
key: 'naive',
name: 'Naive'
},
{
key: 'naughty',
name: 'Naughty'
},
{
key: 'quiet',
name: 'Quiet'
},
{
key: 'quirky',
name: 'Quirky'
},
{
key: 'rash',
name: 'Rash'
},
{
key: 'relaxed',
name: 'Relaxed'
},
{
key: 'sassy',
name: 'Sassy'
},
{
key: 'serious',
name: 'Serious'
},
{
key: 'timid',
name: 'Timid'
}
]);

test('GIVEN a limit of 5 and offset of 5 THEN returns the second 5 Natures', async () => {
const { data } = await executeGraphQL<'getAllNatures'>({
query: getAllNatures,
variables: { take: 5, offset: 5 }
});

expect(data.getAllNatures).toEqual([
{
key: 'careful',
name: 'Careful'
},
{
key: 'docile',
name: 'Docile'
},
{
key: 'gentle',
name: 'Gentle'
},
{
key: 'hardy',
name: 'Hardy'
},
{
key: 'hasty',
name: 'Hasty'
}
]);
});

test('GIVEN a limit of 5 and reverse THEN returns the last 5 Natures', async () => {
const { data } = await executeGraphQL<'getAllNatures'>({
query: getAllNatures,
variables: { take: 5, reverse: true }
});

expect(data.getAllNatures).toEqual([
{
key: 'adamant',
name: 'Adamant'
},
{
key: 'bashful',
name: 'Bashful'
},
{
key: 'bold',
name: 'Bold'
},
{
key: 'brave',
name: 'Brave'
},
{
key: 'calm',
name: 'Calm'
}
]);
});

test('GIVEN no parameters THEN returns all Natures', async () => {
const { data } = await executeGraphQL<'getAllNatures'>({
query: getAllNatures,
variables: {}
});

expect(data.getAllNatures).toEqual([
{
key: 'adamant',
name: 'Adamant'
},
{
key: 'bashful',
name: 'Bashful'
},
{
key: 'bold',
name: 'Bold'
},
{
key: 'brave',
name: 'Brave'
},
{
key: 'calm',
name: 'Calm'
},
{
key: 'careful',
name: 'Careful'
},
{
key: 'docile',
name: 'Docile'
},
{
key: 'gentle',
name: 'Gentle'
},
{
key: 'hardy',
name: 'Hardy'
},
{
key: 'hasty',
name: 'Hasty'
},
{
key: 'impish',
name: 'Impish'
},
{
key: 'jolly',
name: 'Jolly'
},
{
key: 'lax',
name: 'Lax'
},
{
key: 'lonely',
name: 'Lonely'
},
{
key: 'mild',
name: 'Mild'
},
{
key: 'modest',
name: 'Modest'
},
{
key: 'naive',
name: 'Naive'
},
{
key: 'naughty',
name: 'Naughty'
},
{
key: 'quiet',
name: 'Quiet'
},
{
key: 'quirky',
name: 'Quirky'
},
{
key: 'rash',
name: 'Rash'
},
{
key: 'relaxed',
name: 'Relaxed'
},
{
key: 'sassy',
name: 'Sassy'
},
{
key: 'serious',
name: 'Serious'
},
{
key: 'timid',
name: 'Timid'
}
]);
});
});
});
4 changes: 2 additions & 2 deletions tests/testUtils/queries/natures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const getNatureWithFullData = gql`
`;

export const getAllNatures = gql`
query () {
getAllNatures() {
{
getAllNatures {
key
name
}
Expand Down

0 comments on commit be4e803

Please sign in to comment.