Skip to content

Commit

Permalink
only generate TS localtext proxy for objects - dont generate for valu…
Browse files Browse the repository at this point in the history
…es (#6870)

add tests for localtext proxy
call localtext when proxy tries to access null keys
  • Loading branch information
furkanevran committed Aug 8, 2023
1 parent f8f3096 commit c1bc580
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
73 changes: 73 additions & 0 deletions packages/corelib/src/q/localtext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
beforeEach(() => {
jest.resetModules();
jest.unmock('./system');
});

const mockLocalTextStore = (localTexts: Record<string, any>) => {
jest.mock('./system', () => ({
getStateStore: jest.fn(() => localTexts)
}));
}



describe('proxyTexts', () => {
it('proxies simple object', async () => {
mockLocalTextStore({
'a.b': 'Abc',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {a: {}}) as any;

expect(texts.a.b).toEqual('Abc');
});

it('proxies nested object', async () => {
mockLocalTextStore({
'a.b.c': 'Ab12c',
'a.c': 'A1c',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {
a: {
b: {}
}
}) as any;

expect(texts.a.b.c).toEqual('Ab12c');
expect(texts.a.c).toEqual('A1c');
});

it('proxies multiple nested object', async () => {
mockLocalTextStore({
'a.b.c': 'Ab12c',
'a.c': 'A1c',
'b.c.d': 'B1cd',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {
a: {
b: {}
},
b: {
c: {}
}
}) as any;

expect(texts.b.c.d).toEqual('B1cd');
});

it('proxies single level object', async () => {
mockLocalTextStore({
'a': 'Abc',
});

const proxyTexts = (await import('./localtext')).proxyTexts;
const texts = proxyTexts({}, '', {}) as any;

expect(texts.a).toEqual('Abc');
});
});
2 changes: 0 additions & 2 deletions packages/corelib/src/q/localtext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export function proxyTexts(o: Record<string, any>, p: string, t: Record<string,
get: (x: Object, y: string) => {
var tv = t[y];
if (tv == null)
return;
if (typeof tv == 'number')
return localText(p + y);
else {
var z = o[y];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ protected void GenerateTexts(bool module)
var jw = new JsonTextWriter(new System.IO.StringWriter(jwBuilder))
#endif
{
QuoteName = false
QuoteName = false,
Formatting = Formatting.Indented,
Indentation = 4
};
jw.WriteStartObject();
List<string> stack = new();
Expand Down Expand Up @@ -166,8 +168,6 @@ protected void GenerateTexts(bool module)
}
else
{
jw.WritePropertyName(part);
jw.WriteValue(1);
cw.Indented("export const ");
sb.Append(part);
sb.AppendLine(": string;");
Expand All @@ -191,7 +191,7 @@ protected void GenerateTexts(bool module)
else
sb.Append(@"['Texts'] = Q.proxyTexts(Texts, '', ");
jw.Flush();
sb.Append(jwBuilder);
sb.Append(string.Join("\n ", jwBuilder.ToString().Split('\n')));
sb.AppendLine(") as any;");
});

Expand Down

0 comments on commit c1bc580

Please sign in to comment.