|
| 1 | +import { |
| 2 | + ErrorCode, |
| 3 | + UserInputError, |
| 4 | +} from 'src/engine/core-modules/graphql/utils/graphql-errors.util'; |
1 | 5 | import { getDomainNameByEmail } from 'src/utils/get-domain-name-by-email'; |
2 | 6 |
|
3 | 7 | describe('getDomainNameByEmail', () => { |
4 | 8 | it('should return the domain name for a valid email', () => { |
5 | 9 | expect(getDomainNameByEmail('user@example.com')).toBe('example.com'); |
6 | 10 | }); |
7 | 11 |
|
8 | | - it('should throw an error if email is empty', () => { |
9 | | - expect(() => getDomainNameByEmail('')).toThrow('Email is required'); |
| 12 | + it('should throw a UserInputError if email is empty', () => { |
| 13 | + expect(() => getDomainNameByEmail('')).toThrow(UserInputError); |
| 14 | + expect(() => getDomainNameByEmail('')).toThrow( |
| 15 | + 'Email is required. Please provide a valid email address.', |
| 16 | + ); |
| 17 | + |
| 18 | + try { |
| 19 | + getDomainNameByEmail(''); |
| 20 | + } catch (error) { |
| 21 | + expect(error).toBeInstanceOf(UserInputError); |
| 22 | + expect(error.extensions.code).toBe(ErrorCode.BAD_USER_INPUT); |
| 23 | + expect(error.extensions.userFriendlyMessage.message).toContain( |
| 24 | + 'Email is required. Please provide a valid email address.', |
| 25 | + ); |
| 26 | + } |
10 | 27 | }); |
11 | 28 |
|
12 | | - it('should throw an error if email does not contain "@"', () => { |
| 29 | + it('should throw a UserInputError if email does not contain "@"', () => { |
13 | 30 | expect(() => getDomainNameByEmail('userexample.com')).toThrow( |
14 | | - 'Invalid email format', |
| 31 | + UserInputError, |
15 | 32 | ); |
| 33 | + |
| 34 | + try { |
| 35 | + getDomainNameByEmail('userexample.com'); |
| 36 | + } catch (error) { |
| 37 | + expect(error).toBeInstanceOf(UserInputError); |
| 38 | + expect(error.extensions.code).toBe(ErrorCode.BAD_USER_INPUT); |
| 39 | + expect(error.extensions.userFriendlyMessage.message).toContain( |
| 40 | + 'The provided email address is not valid. Please use a standard email format (e.g., user@example.com).', |
| 41 | + ); |
| 42 | + } |
16 | 43 | }); |
17 | 44 |
|
18 | | - it('should throw an error if email has more than one "@"', () => { |
| 45 | + it('should throw a UserInputError if email has more than one "@"', () => { |
19 | 46 | expect(() => getDomainNameByEmail('user@example@com')).toThrow( |
20 | | - 'Invalid email format', |
| 47 | + UserInputError, |
21 | 48 | ); |
| 49 | + |
| 50 | + try { |
| 51 | + getDomainNameByEmail('user@example@com'); |
| 52 | + } catch (error) { |
| 53 | + expect(error).toBeInstanceOf(UserInputError); |
| 54 | + expect(error.extensions.code).toBe(ErrorCode.BAD_USER_INPUT); |
| 55 | + expect(error.extensions.userFriendlyMessage.message).toContain( |
| 56 | + 'The provided email address is not valid. Please use a standard email format (e.g., user@example.com).', |
| 57 | + ); |
| 58 | + } |
22 | 59 | }); |
23 | 60 |
|
24 | | - it('should throw an error if domain part is empty', () => { |
25 | | - expect(() => getDomainNameByEmail('user@')).toThrow('Invalid email format'); |
| 61 | + it('should throw a UserInputError if domain part is empty', () => { |
| 62 | + expect(() => getDomainNameByEmail('user@')).toThrow(UserInputError); |
| 63 | + |
| 64 | + try { |
| 65 | + getDomainNameByEmail('user@'); |
| 66 | + } catch (error) { |
| 67 | + expect(error).toBeInstanceOf(UserInputError); |
| 68 | + expect(error.extensions.code).toBe(ErrorCode.BAD_USER_INPUT); |
| 69 | + expect(error.extensions.userFriendlyMessage.message).toContain( |
| 70 | + 'The provided email address is missing a domain. Please use a standard email format (e.g., user@example.com).', |
| 71 | + ); |
| 72 | + } |
26 | 73 | }); |
27 | 74 |
|
28 | 75 | // Edge cases with weird but potentially valid email formats |
|
0 commit comments