Skip to content

Commit

Permalink
fix(all): filter cocktails by ingredient show handle replacementIds (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-gustafsson authored Jan 4, 2023
1 parent efaa05b commit 48ef3f7
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 20 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.moimob.drinkable"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 11800
versionName "1.18.0"
versionCode 11801
versionName "1.18.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/11801.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
• Fixed filter Cocktails by Ingredient to include substitute ingredients
• Fixed issue with showing mocktails
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default async (): Promise<Config> => {
'^.+\\.(ts|tsx)$': 'ts-jest'
},
testEnvironment: 'jsdom',
moduleDirectories: ['node_modules', 'src']
moduleDirectories: ['node_modules', 'src'],
setupFiles: ['./tests/jest-setup.ts']
};
};
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"postcss-loader": "7.0.2",
"prettier": "^2.7.1",
"prettier-plugin-organize-attributes": "^0.0.5",
"reflect-metadata": "^0.1.13",
"sass": "1.57.1",
"sass-loader": "13.2.0",
"style-loader": "3.3.1",
Expand All @@ -87,4 +88,4 @@
"engines": {
"node": ">=10.12.0"
}
}
}
2 changes: 1 addition & 1 deletion src/components/dialogs/welcome-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CocktailService } from 'services/cocktail-service';

@inject(DialogController, LocalStorageService, CocktailService)
export class WelcomeDialog {
public showMocktails: boolean;
public showMocktails: boolean = false;

public controller: DialogController;
public messuarementSystems = [MessuarementSystem.Imperial, MessuarementSystem.Metric];
Expand Down
12 changes: 12 additions & 0 deletions src/domain/mappers/ingredient-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Ingredient } from 'domain/entities/ingredient';

export class IngredientMapper {
static toIngredientAndReplacementIds(ingredient: Ingredient) {
let ids: string[] = [ingredient.id];

if (ingredient.replacementIds !== undefined) {
ids.push(...ingredient.replacementIds);
}
return ids;
}
}
6 changes: 4 additions & 2 deletions src/modules/cocktails/all-cocktails/all-cocktails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ export class AllCocktails {
}

if (this._filterDialogModel.ingredientFilter !== null) {
cocktails = cocktails.filter(x =>
x.ingredientGroups.some(y => y.ingredientId === this._filterDialogModel.ingredientFilter)
let ingredientIds = this._ingredientService.getIngredientAndReplacementIds(
this._filterDialogModel.ingredientFilter
);

cocktails = cocktails.filter(x => x.ingredientGroups.some(x => ingredientIds.includes(x.ingredientId)));
filterCount++;
}

Expand Down
21 changes: 10 additions & 11 deletions src/services/cocktail-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class CocktailService {
private _cocktails: Cocktail[] = getStaticCocktails();
private _createdCocktails: Cocktail[] = [];
private _cocktailInformation: CocktailInformation[] = [];
private _tempMocktails: Cocktail[] = [];
private _mocktails: Cocktail[] = [];
private _highestId = 0;
constructor(private _localStorageService: LocalStorageService, private _ingredientService: IngredientService) {
this._createdCocktails = this._localStorageService.getCocktails();
Expand All @@ -34,16 +34,13 @@ export class CocktailService {
}
});

this._mocktails = this._cocktails.filter(x => x.category === DrinkCategory.Mocktail);

if (this._localStorageService.getSettings().showMocktails !== true) {
this.populateTempMocktails();
this.hideMocktails();
}
}

private populateTempMocktails() {
this._tempMocktails = this._cocktails.filter(x => x.category === DrinkCategory.Mocktail);
this._cocktails = this._cocktails.filter(x => !this._tempMocktails.map(y => y.id).includes(x.id));
}

public getCocktails() {
return [...this._cocktails].sort((a, b) => a.name?.localeCompare(b.name));
}
Expand Down Expand Up @@ -156,11 +153,10 @@ export class CocktailService {
}

public updateShowMocktails(value: boolean) {
if (value) {
this._cocktails.push(...this._tempMocktails);
this._tempMocktails = [];
if (value === true) {
this._cocktails.push(...this._mocktails);
} else {
this.populateTempMocktails();
this.hideMocktails();
}
}

Expand Down Expand Up @@ -188,4 +184,7 @@ export class CocktailService {

return false;
}
private hideMocktails() {
this._cocktails = this._cocktails.filter(x => !this._mocktails.map(y => y.id).includes(x.id));
}
}
14 changes: 12 additions & 2 deletions src/services/ingredient-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CreatedIngredientModel, Ingredient, ManageIngredientModel } from 'domai
import { SpiritType } from 'domain/enums/spirit-type';
import { getStaticIngredients } from 'data/ingredient-data';
import { LocalStorageService } from './local-storage-service';
import { IngredientMapper } from 'domain/mappers/ingredient-mapper';

@inject(LocalStorageService, I18N)
export class IngredientService {
Expand Down Expand Up @@ -61,7 +62,7 @@ export class IngredientService {
}

public getIngredientById(id: string): Ingredient {
return this.getIngredients().find(x => x.id === id);
return this._ingredients.find(x => x.id === id);
}

public getIngredientsBySpiritType(spirit: SpiritType): Ingredient[] {
Expand Down Expand Up @@ -104,7 +105,7 @@ export class IngredientService {
}));
}

public async createIngredient(name: string): Promise<Ingredient> {
public async createIngredient(name: string) {
const ingredient = new Ingredient();
ingredient.name = name;
ingredient.id = this.setIngredientId();
Expand Down Expand Up @@ -140,6 +141,15 @@ export class IngredientService {
this._ingredients = this._ingredients.filter(x => x.id !== id);
}

public getIngredientAndReplacementIds(id: string): string[] {
let ingredient = this._ingredients.find(x => x.id === id);
if (ingredient === undefined) {
return [];
}

return IngredientMapper.toIngredientAndReplacementIds(ingredient);
}

private setIngredientId(): string {
this._highestId++;
return 'x-' + this._highestId;
Expand Down
1 change: 1 addition & 0 deletions tests/jest-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'reflect-metadata';
21 changes: 21 additions & 0 deletions tests/mappers/ingredient-mapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getStaticIngredients } from 'data/ingredient-data';
import { Ingredient } from 'domain/entities/ingredient';
import { IngredientMapper } from 'domain/mappers/ingredient-mapper';

describe('IngredientMapper', () => {
test('toIngredientAndReplacementIds - No replacementIds', () => {
let ingredient = getStaticIngredients()[0] as Ingredient;

let result = IngredientMapper.toIngredientAndReplacementIds(ingredient);

expect(result).toStrictEqual(['1']);
});

test('toIngredientAndReplacementIds - With replacementIds', () => {
let ingredient = getStaticIngredients()[18] as Ingredient;

let result = IngredientMapper.toIngredientAndReplacementIds(ingredient);

expect(result).toStrictEqual(['19', '38', '92']);
});
});
77 changes: 77 additions & 0 deletions tests/services/ingredient-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { IngredientService } from 'services/ingredient-service';
import { LocalStorageService } from 'services/local-storage-service';
import { I18N } from 'aurelia-i18n';

describe('IngredientService', () => {
let localStorageService: LocalStorageService;
let sut: IngredientService;

beforeEach(async () => {
localStorageService = new LocalStorageService();
await localStorageService.initialize();

let i18n = new I18N(null, null);
jest.spyOn(i18n, 'tr').mockReturnValue('name');
sut = new IngredientService(localStorageService, i18n);
});

afterEach(() => {
window.localStorage.clear();
});

describe('GET', () => {
test('Get Ingredients - No initial state', () => {
expect(sut.getIngredients().length).toBeGreaterThan(0);
expect(sut.getCreatedIngredients()).toHaveLength(0);
});

test('Get Random Ingredients', () => {
expect(sut.getRandomIngredients(0)).toHaveLength(0);
expect(sut.getRandomIngredients(3)).toHaveLength(3);
});

test('getIngredientAndReplacementIds - Ingredient not found', () => {
expect(sut.getIngredientAndReplacementIds('error')).toStrictEqual([]);
});
});

describe('Create', () => {
test('Create Ingredient', async () => {
let key = 'CapacitorStorage.ingredients';
expect(window.localStorage.getItem(key)).toBeNull();

let ingredient = await sut.createIngredient('Test');
expect(ingredient.id).toBe('x-1');

expect(window.localStorage.getItem(key)).toBeTruthy();
expect(sut.getCreatedIngredients()).toStrictEqual([ingredient]);
expect(sut.getIngredients()).toContain(ingredient);
});
});

describe('Update', () => {
test('Update Ingredient', async () => {
let ingredient = await sut.createIngredient('Test');

let updatedIngredient = { ...ingredient };
updatedIngredient.name = 'updated';

await sut.updateIngredient(updatedIngredient);

expect(ingredient.id).toBe('x-1');
expect(sut.getCreatedIngredients()).toStrictEqual([updatedIngredient]);
expect(sut.getIngredients()).toContain(updatedIngredient);
});
});

describe('Delete', () => {
test('Delete Ingredient', async () => {
let ingredient = await sut.createIngredient('Test');

await sut.deleteIngredient(ingredient.id);

expect(sut.getCreatedIngredients()).toStrictEqual([]);
expect(sut.getIngredientById(ingredient.id)).toBeUndefined();
});
});
});

0 comments on commit 48ef3f7

Please sign in to comment.