Skip to content

Commit

Permalink
Wait to do typechecking when we have a schema
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydew committed Feb 23, 2023
1 parent 6842836 commit 5a4f462
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ describe('SavedArticlesPage', () => {
});
});

it('should display an error message if the API call is successful but the json response is not expected', async () => {
// eslint-disable-next-line jest/no-disabled-tests -- Add this test in when we have validated the response type
it.skip('should display an error message if the API call is successful but the json response is not expected', async () => {
const invalidJson = {
foo: 'bar',
};
Expand Down
27 changes: 4 additions & 23 deletions client/components/mma/savedArticles/models/SavedArticle.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import { isObject } from '@guardian/libs';

export interface SavedArticle {
id: string;
shortUrl: string;
date: Date;
read: boolean;
}

// TODO - more extensible way of type guarding
export function isSavedArticle(data: unknown): data is SavedArticle {
const castObj = data as SavedArticle;
return (
castObj !== null &&
castObj !== undefined &&
'id' in castObj &&
'shortUrl' in castObj &&
'date' in castObj &&
'read' in castObj
);
}
export interface SavedArticlesResponse {
version: number;
articles: SavedArticle[];
}
export function isSavedArticlesResponse(
data: unknown,
): data is SavedArticlesResponse {
const castObj = data as SavedArticlesResponse;
return (
castObj !== null &&
castObj !== undefined &&
'version' in castObj &&
'articles' in castObj &&
castObj.articles.reduce<boolean>((b, sv) => {
return isSavedArticle(sv) && b;
}, true)
);
// TODO - validate this properly when we have the schema
return isObject(data);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"@guardian/ab-core": "^2.0.0",
"@guardian/ab-react": "^2.0.1",
"@guardian/consent-management-platform": "^6.11.3",
"@guardian/libs": "^1.7.1",
"@guardian/libs": "^13.1.0",
"@guardian/source-foundations": "^5.0.0",
"@guardian/source-react-components": "^5.0.0",
"@sentry/browser": "^5.22.3",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2429,10 +2429,10 @@
"@typescript-eslint/eslint-plugin" "5.21.0"
"@typescript-eslint/parser" "5.21.0"

"@guardian/libs@^1.7.1":
version "1.7.1"
resolved "https://registry.yarnpkg.com/@guardian/libs/-/libs-1.7.1.tgz#8fe4abd10d4ab5f4cdf397f84f416fe6c698f662"
integrity sha512-gafm04w/ETycDBb8rRDdt+MYhBAiA5aJ5JHYe2s2Q+0fk0npyWDJFvQB0Rttm1qblWlaHimLLfzZNNoAReKwLA==
"@guardian/libs@^13.1.0":
version "13.1.0"
resolved "https://registry.yarnpkg.com/@guardian/libs/-/libs-13.1.0.tgz#5a7a0ec9e2d432fc3176884072b9c7c22a00a95b"
integrity sha512-iffG2zIDmMtIHSesClxmimZIwAce3IBbSEihX0IRK65Lm2TSIfsb8ZRFRsMYDW+GE2h3yKFuU8tqVKvQXpzXQg==

"@guardian/node-riffraff-artifact@^0.2.2":
version "0.2.2"
Expand Down

0 comments on commit 5a4f462

Please sign in to comment.