Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spec to mmp.service.ts #580

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: lint, change: remove unknown test args
  • Loading branch information
sorenjohanson committed Nov 20, 2024
commit b53eef8de9d360f669b1ccb7989ebd9ee365dab3
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: npm --prefix teammapper-frontend ci
- run: npm --prefix teammapper-frontend run test -- --progress=false --watch=false --code-coverage --source-map=false
- run: npm --prefix teammapper-frontend run test
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const mockMmpService = {
new: jest.fn(),
addNodeImage: jest.fn(),
map: {
on: jest.fn(),
remove: jest.fn(),
center: jest.fn(),
new: jest.fn(),
addNodeImage: jest.fn(),
map: {
on: jest.fn(),
remove: jest.fn(),
center: jest.fn(),
new: jest.fn(),
},
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ describe('PictogramService', () => {

settingsService = {
getCachedSettings: jest.fn().mockReturnValue({
general: { language: 'en' }
})
general: { language: 'en' },
}),
} as any;

service = new PictogramService(httpClient, settingsService);
});

it('fetches pictos', (done) => {
it('fetches pictos', done => {
const searchTerm = 'House';
const expectedUrl = 'https://api.arasaac.org/v1/pictograms/en/search/House';
httpClient.get.mockReturnValue(of([testData]));
Expand All @@ -61,19 +61,21 @@ describe('PictogramService', () => {
);
});

it('gets the image', (done) => {
it('gets the image', done => {
const blob = new Blob();
const expectedUrl = 'https://static.arasaac.org/pictograms/3/3_300.png';
httpClient.get.mockReturnValue(of(blob));

service.getPictoImage(3).subscribe(data => {
expect(data).toEqual(blob);
expect(httpClient.get).toHaveBeenCalledWith(expectedUrl, { responseType: 'blob' });
expect(httpClient.get).toHaveBeenCalledWith(expectedUrl, {
responseType: 'blob',
});
done();
});
});

it('uses default language when settings are not available', (done) => {
it('uses default language when settings are not available', done => {
settingsService.getCachedSettings.mockReturnValue(null);
const searchTerm = 'House';
const expectedUrl = 'https://api.arasaac.org/v1/pictograms/en/search/House';
Expand All @@ -92,4 +94,4 @@ describe('PictogramService', () => {
'https://static.arasaac.org/pictograms/3/3_500.jpg'
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ describe('SettingsService', () => {
adminId: '123',
rootName: 'test',
};

storageService.getAllCreatedMapsFromStorage.mockResolvedValue([
['123', cachedMapDataFromStorage]
['123', cachedMapDataFromStorage],
]);

// Test
const result = await settingsService.getCachedAdminMapEntries();

// Assert
expect(result).toEqual([]);
expect(storageService.getAllCreatedMapsFromStorage).toHaveBeenCalled();
Expand All @@ -59,14 +59,14 @@ describe('SettingsService', () => {
futureDateOne.setFullYear(new Date().getFullYear() + 2);
const futureDateTwo = new Date();
futureDateTwo.setFullYear(new Date().getFullYear() + 1);

const cachedMapDataFromStorage: CachedAdminMapValue = {
ttl: futureDateOne,
modificationSecret: '456',
adminId: '123',
rootName: 'test',
};

const otherCachedMapDataFromStorage: CachedAdminMapValue = {
ttl: futureDateTwo,
modificationSecret: '456',
Expand All @@ -76,16 +76,16 @@ describe('SettingsService', () => {

storageService.getAllCreatedMapsFromStorage.mockResolvedValue([
['789', otherCachedMapDataFromStorage],
['123', cachedMapDataFromStorage]
['123', cachedMapDataFromStorage],
]);

// Test
const result = await settingsService.getCachedAdminMapEntries();

// Assert
expect(result).toEqual([
{ id: '123', cachedAdminMapValue: cachedMapDataFromStorage },
{ id: '789', cachedAdminMapValue: otherCachedMapDataFromStorage }
{ id: '789', cachedAdminMapValue: otherCachedMapDataFromStorage },
]);
expect(storageService.getAllCreatedMapsFromStorage).toHaveBeenCalled();
});
Expand All @@ -94,29 +94,35 @@ describe('SettingsService', () => {
describe('init', () => {
it('initializes settings with default values when no cached settings exist', async () => {
const defaultSettings = { language: 'en' };

httpService.get.mockResolvedValue({
json: () => Promise.resolve(defaultSettings)
json: () => Promise.resolve(defaultSettings),
});
storageService.get.mockResolvedValue(null);

await settingsService.init();

expect(storageService.set).toHaveBeenCalledWith('settings', defaultSettings);

expect(storageService.set).toHaveBeenCalledWith(
'settings',
defaultSettings
);
});

it('initializes settings with cached values when they exist', async () => {
const defaultSettings = { language: 'en' };
const cachedSettings = { language: 'fr' };

httpService.get.mockResolvedValue({
json: () => Promise.resolve(defaultSettings)
json: () => Promise.resolve(defaultSettings),
});
storageService.get.mockResolvedValue(cachedSettings);

await settingsService.init();

expect(storageService.set).toHaveBeenCalledWith('settings', cachedSettings);

expect(storageService.set).toHaveBeenCalledWith(
'settings',
cachedSettings
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('FooterComponent', () => {
fontMaxSize: 16,
fontMinSize: 12,
fontIncrement: 2,
}
},
};

beforeEach(async () => {
mockSettingsService = {
getCachedSettings: jest.fn().mockReturnValue(mockSettings),
Expand All @@ -41,15 +41,15 @@ describe('FooterComponent', () => {
declarations: [FooterComponent],
providers: [
{ provide: SettingsService, useValue: mockSettingsService },
{ provide: TranslateService, useValue: mockTranslateService }
{ provide: TranslateService, useValue: mockTranslateService },
],
imports: [
TranslateModule.forRoot({
defaultLanguage: 'en',
}),
MatIconModule,
MatSelectModule,
BrowserAnimationsModule
BrowserAnimationsModule,
],
}).compileComponents();

Expand All @@ -65,7 +65,16 @@ describe('FooterComponent', () => {

it('should initialize with correct settings and languages', () => {
expect(mockSettingsService.getCachedSettings).toHaveBeenCalled();
expect(component.languages).toEqual(['en', 'fr', 'de', 'it', 'zh-tw', 'zh-cn', 'es', 'pt-br']);
expect(component.languages).toEqual([
'en',
'fr',
'de',
'it',
'zh-tw',
'zh-cn',
'es',
'pt-br',
]);
expect(component.currentYear).toBe(new Date().getFullYear().toString());
});
});
Expand All @@ -74,12 +83,14 @@ describe('FooterComponent', () => {
it('should update language', async () => {
const newSettings = {
...mockSettings,
general: { language: 'fr' }
general: { language: 'fr' },
};
component.settings = newSettings;
await component.updateLanguage();

expect(mockSettingsService.updateCachedSettings).toHaveBeenCalledWith(newSettings);
expect(mockSettingsService.updateCachedSettings).toHaveBeenCalledWith(
newSettings
);
expect(mockTranslateService.use).toHaveBeenCalledWith('fr');
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,32 @@ describe('DialogPictogramsComponent', () => {
let mockUtilsService: jest.Mocked<UtilsService>;
let mockPictoService: jest.Mocked<PictogramService>;

const mockPictogramResponse: IPictogramResponse[] = [{
_id: 1,
keywords: [{
keyword: 'test',
type: 1,
plural: 'tests',
hasLocation: false
}],
schematic: false,
sex: false,
violence: false,
aac: false,
aacColor: false,
skin: false,
hair: false,
downloads: 0,
categories: ['test'],
synsets: ['test'],
tags: ['test'],
created: new Date(),
lastUpdated: new Date()
}];
const mockPictogramResponse: IPictogramResponse[] = [
{
_id: 1,
keywords: [
{
keyword: 'test',
type: 1,
plural: 'tests',
hasLocation: false,
},
],
schematic: false,
sex: false,
violence: false,
aac: false,
aacColor: false,
skin: false,
hair: false,
downloads: 0,
categories: ['test'],
synsets: ['test'],
tags: ['test'],
created: new Date(),
lastUpdated: new Date(),
},
];

beforeEach(async () => {
mockMmpService = {
Expand Down Expand Up @@ -93,7 +97,7 @@ describe('DialogPictogramsComponent', () => {

it('should update pictos when search is called', async () => {
mockPictoService.getPictos.mockReturnValue(of(mockPictogramResponse));

await component.search();
expect(component.pictos).toEqual(mockPictogramResponse);
});
Expand All @@ -102,9 +106,9 @@ describe('DialogPictogramsComponent', () => {
const testId = 123;
const mockUrl = 'test-url';
mockPictoService.getPictoImageUrl.mockReturnValue(mockUrl);

const result = component.getImageUrlOfId(testId);
expect(mockPictoService.getPictoImageUrl).toHaveBeenCalledWith(testId);
expect(result).toBe(mockUrl);
});
});
});
Loading
Loading