Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Dec 16, 2024
1 parent e024ba3 commit b279402
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 46 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@
"@types/express": "^4.17.13",
"@types/graphql-fields": "^1.3.6",
"@types/graphql-upload": "^8.0.12",
"@types/jest": "^29.5.11",
"@types/js-cookie": "^3.0.3",
"@types/js-levenshtein": "^1.1.3",
"@types/lodash.camelcase": "^4.3.7",
Expand Down
32 changes: 32 additions & 0 deletions packages/twenty-shared/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { JestConfigWithTsJest, pathsToModuleNameMapper } from 'ts-jest';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsConfig = require('./tsconfig.json');

const jestConfig: JestConfigWithTsJest = {
displayName: 'twenty-ui',
preset: '../../jest.preset.js',
testEnvironment: 'jsdom',
transformIgnorePatterns: ['../../node_modules/'],
transform: {
'^.+\\.[tj]sx?$': [
'@swc/jest',
{
jsc: {
parser: { syntax: 'typescript', tsx: true },
transform: { react: { runtime: 'automatic' } },
},
},
],
},
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|webp|svg|svg\\?react)$':
'<rootDir>/__mocks__/imageMock.js',
...pathsToModuleNameMapper(tsConfig.compilerOptions.paths),
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
extensionsToTreatAsEsm: ['.ts', '.tsx'],
coverageDirectory: './coverage',
};

export default jestConfig;
3 changes: 0 additions & 3 deletions packages/twenty-shared/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"name": "twenty-shared",
"version": "0.40.0-canary",
"description": "",
"author": "",
"private": true,
"license": "AGPL-3.0",
"main": "./dist/index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions packages/twenty-shared/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "twenty-shared",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/twenty-shared/src",
"projectType": "library",
"tags": ["scope:shared"],
"targets": {
Expand All @@ -11,6 +12,7 @@
}
},
"typecheck": {},
"test": {},
"lint": {
"options": {
"lintFilePatterns": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { getImageAbsoluteURI } from '../getImageAbsoluteURI';

describe('getImageAbsoluteURI', () => {
it('should return null if imageUrl is empty', () => {
it('should return baseUrl if imageUrl is empty string', () => {
const imageUrl = '';
const baseUrl = 'http://localhost:3000';
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
expect(result).toBeNull();
});

it('should return null if baseUrl is empty', () => {
const imageUrl = 'https://XXX';
const baseUrl = '';
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
expect(result).toBeNull();
expect(result).toBe('http://localhost:3000/files/');
});

it('should return absolute url if the imageUrl is an absolute url', () => {
Expand All @@ -22,24 +15,24 @@ describe('getImageAbsoluteURI', () => {
expect(result).toBe(imageUrl);
});

it('should return fully formed url if imageUrl is a url', () => {
const imageUrl = 'pic.png?token=XXX';
it('should return fully formed url if imageUrl is a relative url starting with /', () => {
const imageUrl = '/path/pic.png';
const baseUrl = 'http://localhost:3000';
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
expect(result).toBe('http://localhost:3000/files/pic.png?token=XXX');
expect(result).toBe('http://localhost:3000/files/path/pic.png');
});

it('should return fully formed url if imageUrl is a relative url', () => {
const imageUrl = '/pic.png?token=XXX';
it('should return fully formed url if imageUrl is a relative url nost starting with slash', () => {
const imageUrl = 'pic.png';
const baseUrl = 'http://localhost:3000';
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
expect(result).toBe('http://localhost:3000/files/pic.png?token=XXX');
expect(result).toBe('http://localhost:3000/files/pic.png');
});

it('should return null if imageUrl is an empty string', () => {
const imageUrl = '';
it('should handle queryParameters in the imageUrl', () => {
const imageUrl = '/pic.png?token=XXX';
const baseUrl = 'http://localhost:3000';
const result = getImageAbsoluteURI({ imageUrl, baseUrl });
expect(result).toBeNull();
expect(result).toBe('http://localhost:3000/files/pic.png?token=XXX');
});
});
9 changes: 2 additions & 7 deletions packages/twenty-shared/src/utils/image/getImageAbsoluteURI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ export const getImageAbsoluteURI = ({
return imageUrl;
}

const absoluteImageUrl = new URL(baseUrl);

if (imageUrl.startsWith('/')) {
absoluteImageUrl.pathname = `/files${imageUrl}`;
return absoluteImageUrl.toString();
return new URL(`/files${imageUrl}`, baseUrl).toString();
}

absoluteImageUrl.pathname = `files/${imageUrl}`;

return absoluteImageUrl.toString();
return new URL(`/files/${imageUrl}`, baseUrl).toString();
};
2 changes: 1 addition & 1 deletion packages/twenty-shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"path": "./tsconfig.spec.json"
},
}
],
"extends": "../../tsconfig.base.json"
}
4 changes: 1 addition & 3 deletions packages/twenty-shared/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/__mocks__/**/*",
"jest.config.ts",
"setupTests.ts",
"src/**/*.d.ts",
"src/**/*.spec.ts",
"src/**/*.spec.tsx",
"src/**/*.test.ts",
"src/**/*.test.tsx",
"tsup.config.ts",
"tsup.ui.index.tsx",
"vite.config.ts"
]
}
15 changes: 2 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15925,16 +15925,6 @@ __metadata:
languageName: node
linkType: hard

"@types/jest@npm:^29.5.11":
version: 29.5.12
resolution: "@types/jest@npm:29.5.12"
dependencies:
expect: "npm:^29.0.0"
pretty-format: "npm:^29.0.0"
checksum: 10c0/25fc8e4c611fa6c4421e631432e9f0a6865a8cb07c9815ec9ac90d630271cad773b2ee5fe08066f7b95bebd18bb967f8ce05d018ee9ab0430f9dfd1d84665b6f
languageName: node
linkType: hard

"@types/js-cookie@npm:^2.2.6":
version: 2.2.7
resolution: "@types/js-cookie@npm:2.2.7"
Expand Down Expand Up @@ -26153,7 +26143,7 @@ __metadata:
languageName: node
linkType: hard

"expect@npm:^29.0.0, expect@npm:^29.7.0":
"expect@npm:^29.7.0":
version: 29.7.0
resolution: "expect@npm:29.7.0"
dependencies:
Expand Down Expand Up @@ -38284,7 +38274,7 @@ __metadata:
languageName: node
linkType: hard

"pretty-format@npm:^29.0.0, pretty-format@npm:^29.5.0, pretty-format@npm:^29.7.0":
"pretty-format@npm:^29.5.0, pretty-format@npm:^29.7.0":
version: 29.7.0
resolution: "pretty-format@npm:29.7.0"
dependencies:
Expand Down Expand Up @@ -44173,7 +44163,6 @@ __metadata:
"@types/facepaint": "npm:^1.2.5"
"@types/graphql-fields": "npm:^1.3.6"
"@types/graphql-upload": "npm:^8.0.12"
"@types/jest": "npm:^29.5.11"
"@types/js-cookie": "npm:^3.0.3"
"@types/js-levenshtein": "npm:^1.1.3"
"@types/lodash.camelcase": "npm:^4.3.7"
Expand Down

0 comments on commit b279402

Please sign in to comment.